Jquery判断input获取和失去焦点

在一些表单中,难免会碰到用户的输入是在进行中,还是输入完成
接下来,我会告诉你们,jquery如何灵活的判断input输入框是在输入中还是输入完成
主要还是判断他是否获得焦点

HTML
index.html

<form action="" method="post">

    <h2>用户登陆</h2>

    <span>用户名</span><input type="text" value="请输 入你的用户名" name="username" id="username">

    <span>密  码</span><input  type="password" value="" name="password"  id="password">

    <br/>

    <button type="submit" id="login">登陆</button>

</form>

调用外部cdn的jquery
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>

<script>
<script>
//    获取用户名

//    获取焦点
$("#username").focus(function() {
//获取当前文本框的值
    var curValue = $(this).val();
    //console.log(curValue);
    //可以打印输入结果curValue
    if (curValue == this.defaultValue) {
        $(this).val("");
    }
});


//失去焦点
$("#username").blur(function() {
    var curValue = $("#username").val();
     //console.log(curValue);
    //可以打印输入结果curValue
    if($.trim(curValue)==""){
        $(this).val(this.defaultValue);
    }
});

//输入框正在输入时
$("#username").on('input',function(){
    if(!($('#username').val()=='')){
    }else{
    }
});
</script>

jquery判断input输入框的值,用到input事件,blur事件,focus事件
转载请填写
https://blog.csdn.net/qq_41716624/article/details/93145092

发布了30 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41716624/article/details/93145092