2
$(document).ready(function() {
$("#textboxA").keyup(function () {
var message;
message ="Currently Textbox A in use";
$("#error").slideDown("fast");
document.getElementById('error').innerHTML=message;

});

$("#textboxB").keyup(function () {
var message;
message ="Currently Textbox B in use";
$("#error").slideDown("fast");
document.getElementById('error').innerHTML=message;

});    

});

在我上面的代码中,每当我按 Tab 键从我的 textboxA 移动到 textboxB 时,jquery 函数都会触发并显示消息。如何预防?

小提琴设置在这里

4

2 回答 2

3

检查以确保未按下制表键。

$(document).ready(function() {
    $("#textboxA").keyup(function (e) {
        if(e.keyCode != 9){
             var message;
             message ="Currently Textbox A in use";
             $("#error").slideDown("fast");
             document.getElementById('error').innerHTML=message;

             });

       $("#textboxB").keyup(function (e) {
             if(e.keyCode != 9){
             var message;
             message ="Currently Textbox B in use";
             $("#error").slideDown("fast");
             document.getElementById('error').innerHTML=message;

             });  
             }

       }
});
于 2013-10-19T05:40:31.477 回答
1

简单检查选项卡keyCode。如果它不是选项卡键,则执行。这是代码

$(document).ready(function () {
    $("#textboxA").keyup(function (e) {
        var message;
        message = "Currently Textbox A in use";
        if (e.keyCode != 9) {
            $("#error").slideDown("fast");
            document.getElementById('error').innerHTML = message;
        }
    });

    $("#textboxB").keyup(function (e) {
        var message;
        message = "Currently Textbox B in use";
        if (e.keyCode != 9) {
            $("#error").slideDown("fast");
            document.getElementById('error').innerHTML = message;
        }
    });

});

这是演示

于 2013-10-19T05:43:37.047 回答