Javascript 执行某个动作后如何自动触发回车键按下事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11927078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:42:02  来源:igfitidea点击:

How can I automatically trigger an enter key press event after a certain action is executed?

javascriptjqueryhtml

提问by Ellie Fabrero

I'm writing some unit test in my web apps and I want to automatically trigger an Enterkey pressed event from my page after for example a download to text file button was clicked. How can i do this in jQuery or Javascript?

我正在我的网络应用程序中编写一些单元测试,并且我想Enter在例如单击下载到文本文件按钮后从我的页面自动触发一个按键事件。我怎样才能在 jQuery 或 Javascript 中做到这一点?

采纳答案by Oriol

  1. Create a variable that tells us if the user has clicked your button.

    var clicked = false;
    
  2. Add an event listener to your button, so that when the user clicks on it, the clickedvariable will become true.

    myButton.addEventListener('click', function(){
      clicked = true;
    });
    
  3. Add a keypressevent listener:

    document.addEventListener('keypress', function(e) {
      // `e` is the event
    });
    
  4. Inside that event listener, check if the user clicked previously

    if(clicked) {
      // ...
    }
    
  5. If (s)he did, check the pressed key. Enter's key code is 13.

    var keynum = e.keyCode||e.which;
    if(keynum == 13) {
       // ...
    }
    
  6. If the pressed key was enter, use

    clicked = false;
    

    Otherwise, once the user clicked your button and pressed enter, it wouldn't be necessary to click the button again.

  7. After that run your code. For example, this will call function fafter 2 seconds.

    setTimeout(f, 2000);
    
  1. 创建一个变量,告诉我们用户是否点击了您的按钮。

    var clicked = false;
    
  2. 给你的按钮添加一个事件监听器,这样当用户点击它时,clicked变量就会变成true.

    myButton.addEventListener('click', function(){
      clicked = true;
    });
    
  3. 添加keypress事件监听器:

    document.addEventListener('keypress', function(e) {
      // `e` is the event
    });
    
  4. 在该事件侦听器中,检查用户之前是否单击过

    if(clicked) {
      // ...
    }
    
  5. 如果他(她)这样做了,请检查按下的键。Enter 的键码是 13。

    var keynum = e.keyCode||e.which;
    if(keynum == 13) {
       // ...
    }
    
  6. 如果按下的键是输入,请使用

    clicked = false;
    

    否则,一旦用户单击您的按钮并按下 Enter,就不需要再次单击该按钮。

  7. 之后运行你的代码。例如,这将f在 2 秒后调用函数。

    setTimeout(f, 2000);
    

Live demo

现场演示

var clicked = false;
document.querySelector('#btn').addEventListener('click', function(){
  clicked = true;
  snippet.log("You clicked the button. `clicked` is now `true`");
});
document.addEventListener('keypress', function(e) {
  if(clicked) {
    var keynum = e.keyCode || e.which;
    if(keynum == 13) {
      clicked = false; 
      snippet.log("`clicked` is now `false`. Waiting 2 seconds...");
      setTimeout(f, 2000);
    }
  }
});
function f() {
  snippet.log("Function `f` executed successfully!");
}
#btn {
  border: 3px solid red;
  cursor: pointer;
}
<div id="btn">Click me, and then press enter.</div>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --><script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

回答by themhz

If I get your question right, this is what you are looking for

如果我答对了你的问题,这就是你要找的

<html>
<head>

  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script type="text/javascript">
      $(document).keydown(function(event){
            if(event.keyCode == 13){                
                alert("keypressed");
            }
        });

function clickevent()
{                
     var e = $.Event("keydown");
     e.which = 13;
     e.keyCode = 13;
     $(document).trigger(e);     
}
</script>

</head>
<body id="thebody">
  <input type="button" onclick="clickevent()" value="click me first">
</body>
</html>

回答by jeff

If you're talking about automatically entering an Enterkey to trigger a submitevent, you can use this instead:

如果您正在谈论自动输入Enter密钥以触发submit事件,则可以改用它:

$("#formID").submit();



要在按下按钮后触发此事件,请尝试以下操作:

$("#buttonID").click(function() {
    $("#formID").submit(); // submits form
});

回答by themhz

$("#txtfield").keyup(function(e) {
 if(e.keyCode == 13) {
  $("#submit").submit();
 }
});