Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当按下Tab键时,如何停止在keydown上运行jQuery函数_Jquery - Fatal编程技术网

当按下Tab键时,如何停止在keydown上运行jQuery函数

当按下Tab键时,如何停止在keydown上运行jQuery函数,jquery,Jquery,我是jQuery新手,还没有找到解决方案 我有这个HTML: <input type="text" id="Box1" /><br /> <input type="text" id="Box2" /> 示例如下: 当您输入“Box1”然后开始输入“Box2”时,Box1的内容将被清除。这很好,但是如果按下Tab键,我不希望此功能运行,也就是说,如果我按下Box2内的Tab键,我不希望清除Box1。如何在jQuery中编写此命令?单击该键时使用e.preven

我是jQuery新手,还没有找到解决方案

我有这个HTML:

<input type="text" id="Box1" /><br />
<input type="text" id="Box2" />
示例如下:

当您输入“Box1”然后开始输入“Box2”时,Box1的内容将被清除。这很好,但是如果按下Tab键,我不希望此功能运行,也就是说,如果我按下Box2内的Tab键,我不希望清除Box1。如何在jQuery中编写此命令?

单击该键时使用
e.preventDefault()

$("input").keydown(function(e){
    if(e.keyCode == "9"){
        e.preventDefault();
    }
});

检查JSFIDLE。

您需要检查用于启动事件的密钥:

$("#Box1").keydown(function (e) {
    // If the pressed key is not tab (9) then reset Box2
    if(e.keyCode !== 9){
        $("#Box2").val("");
    }
});
$("#Box2").keydown(function (e) {
    // If the pressed key is not tab (9) then reset Box1
    if(e.keyCode !== 9){
        $("#Box1").val("");
    }
});

除了tab键,您可能还希望忽略shift、ctrl、箭头等。谢谢,但我仍然需要能够通过tab键切换到另一个框。我只是不想在按下tab键时清除内容。这可能吗?
$("#Box1").keydown(function (e) {
    // If the pressed key is not tab (9) then reset Box2
    if(e.keyCode !== 9){
        $("#Box2").val("");
    }
});
$("#Box2").keydown(function (e) {
    // If the pressed key is not tab (9) then reset Box1
    if(e.keyCode !== 9){
        $("#Box1").val("");
    }
});