用户名实时更改中keyup()事件和change()事件之间的差异 [英] Differences between keyup() event and change() event in username live change

查看:197
本文介绍了用户名实时更改中keyup()事件和change()事件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 用户名html字段

  • username html field

Username: <input type="text" name="username" id="username" />


  • keyup()事件

  • keyup() event

    $(document).ready(function(){
            $('#username').on('keyup', function(){
               var username = $("#username").val();
               alert(username);
            });
    });
    


  • change()事件

  • change() event

    $(document).ready(function(){
            $("#username").change(function() {
               var username = $("#username").val();
               alert(username);
            });
    });
    


  • 我的问题是我应该使用 keyup() event或 change()要进行用户名验证的事件?

  • My question is should I use keyup() event or change() event to do username verification?

    推荐答案

    它们几乎是一回事。在jQuery或JavaScript中,我不得不推荐 change()事件。 您不应使用 keyup()的原因是因为如果用户使用自动填充输入值,则不会触发 keyup() event。但是,自动填充触发 change()事件,您的验证脚本将会运行,并且输入将被验证。

    They are almost the same thing. In jQuery or JavaScript, I would have to recommend the change() event. The reason you should not use keyup() is because if a user inputs a value using autofill, it will not fire the keyup() event. However, autofill does fire the change() event, and your verification script will run, and the input will be verified.

    注意:通常,更改()在元素失去焦点。如果你想在每次按键后检查数据,你可以组合 keyup()更改(),这将允许您解析两个事件的数据。在我看来,这是两全其美的。

    NOTE: Normally, change() is fired when the element loses focus. If you want to check data after every key press, you could combine keyup() and change(), which would allow you to parse the data on both events. This is the best of both worlds in my opinion.

    希望这会有所帮助!

    这篇关于用户名实时更改中keyup()事件和change()事件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆