jquery 获取input的值-jQuery操作input改变value属性值

2023-08-29 0 2,067 百度已收录

我今天写了一个表单元素。 当用户点击时,输入中的内容将会被清除。 当鼠标点击其他地方时,输入的值将被保存为输入的值。

像这样的效果

jquery 获取input的值-jQuery操作input改变value属性值

当用户单击时,文本消失。

html代码

     
     
     
     

jq码


	$(document).ready(function(e) {
        var temp;
        $(":text").focusin(function(){
            var value = $(this).val();
            if ($(this).val() == "请输入密码" || $(this).val() == "请输入您的邮箱地址" || $(this).val() == "确认密码" || $(this).val() =="请输入用户名") {				
				if($(this).val() == "确认密码" || $(this).val() == "请输入密码") {
					$(this).attr('type','password')
				}
                $(this).val("")
			}
            //alert(value)
		})
		$(":input").focusout(function(event) {
            /* Act on the event */
            if($(this).val() == "") {              
                if ($(this).hasClass('pwd')) {
                    $(this).attr('type','text')
                };
                $(this).val(temp)
            }
        });
    })

这样以后基本需要的功能就可以实现了jquery 获取input的值,但是代码不够堂堂jquery 获取input的值,所以就想到了用链表来保存值,

jquery 获取input的值-jQuery操作input改变value属性值

var arr_ = [];
        var temp;
        $(":text").each(function() {
            arr_.push($(this).val())
        })
        $(":text").focusin(function(){
			var that = this;
            var value = $(that).val();
            temp = value;
            $.each(arr_,function(i,n) {
				if(value==n){
					$(that).val("");
					if(value=="请输入密码"||value=="确认密码"){
						$(that).attr("type","password");
					}
				}
            });
		})

我又发现了一个问题,总是需要一个全局变量temp来保存值,这对javascript来说不好,所以我又想到了data属性

jquery 获取input的值-jQuery操作input改变value属性值


            
            
            

$(document).ready(function(e) {
        var arr_ = [];
        $(":text").each(function() {
            arr_.push($(this).val())
        })
        $(":text").focusin(function(){
			var that = this;
            var value = $(that).val();
            $.each(arr_,function(i,n) {
				if(value==n){
					$(that).val("");
					if(value=="请输入密码"||value=="确认密码"){
						$(that).attr("type","password");
					}
				}
            });
		})
		$(":input").focusout(function(event) {
            /* Act on the event */
            if($(this).val() == "") {              
                if ($(this).hasClass('pwd')) {
                    $(this).attr('type','text')
                };
                $(this).val($(this).attr("data"));
            }
        });
    })

这看起来会舒服很多。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 jquery jquery 获取input的值-jQuery操作input改变value属性值 https://www.wkzy.net/game/182065.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务