选项卡上的Keyup事件行为 [英] Keyup event behavior on tab

查看:105
本文介绍了选项卡上的Keyup事件行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<form id="myform">
    <input id="firstfield" name="firstfield" value="100" type="text" />
    <input id="secondfield" name="secondfield" value="200" type="text" />
</form>

jQuery:

jQuery(document).ready(function() {
    $('#firstfield').keyup(function() {
      alert('Handler for firstfield .keyup() called.');
    });
    $('#secondfield').keyup(function() {
      alert('Handler for secondfield .keyup() called.');
    });
});

演示: http://jsfiddle.net/KtSja/3/

在此演示中,如果将光标放在第一个字段中,然后选项卡(不做任何更改),在第二个字段上触发keyup事件。也就是说,你是从第一场到第二场的标签。这种行为是否正确?我怎样才能防止这种情况发生?同样适用于shift + tab。

In this demo, if you place your cursor in the first field and then tab out (without making any changes), the keyup event is fired on the second field. i.e., you are tabbing out of first field and into second field. Is this behavior correct? How can I prevent this from happening? Same applies to shift + tab.

注意:

a)我相信所有其他键,可打印和非-printable,在第一个字段上触发keyup事件。

a) I believe all other keys, printable and non-printable, trigger the keyup event on the first field.

b)如果按住标签直到它移出两个字段,则根本不会触发事件。

b) The event isn't triggered at all if you keep the tab pressed until it moves out of both fields.

推荐答案

我最终使用了这个解决方案:

I ended up using this solution:

HTML:

<form id="myform">
    <input id="firstfield" name="firstfield" value="100" type="text" />
    <input id="secondfield" name="secondfield" value="200" type="text" />
</form>

jQuery:

jQuery(document).ready(function () {
    $('#firstfield').keyup(function (e) {
        var charCode = e.which || e.keyCode; // for cross-browser compatibility
        if (!((charCode === 9) || (charCode === 16)))
            alert('Handler for firstfield .keyup() called.');
    });
    $('#secondfield').keyup(function (e) {
       var charCode = e.which || e.keyCode; // for cross-browser compatibility
       if (!((charCode === 9) || (charCode === 16)))
            alert('Handler for secondfield .keyup() called.');
    });
});

如果密钥是tab,shift或两者,此解决方案不会运行警报。

This solution doesn't run the alert if the key is tab, shift or both.

解决方案: http://jsfiddle.net/KtSja/13/

这篇关于选项卡上的Keyup事件行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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