简体   繁体   English

通过按Enter的jQuery函数

[英]jQuery function by pressing Enter

I'm struggle with making this work and understand how I can call specific button by pressing Enter in Javascript. 我正在努力进行这项工作,并了解如何通过在Javascript中按Enter来调用特定的按钮。

As Example I have 作为例子,我有

 $(document).ready(function () { $('#myText').keyup(function () { alert(e.keyCode); }); $('#myButton').button().click(function () { alert('Button click 1!'); }); $('#myButton2').button().click(function () { alert('Button click 2!'); }); }); 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <form action="#"> <button id="myButton">Button</button> <button id="myButton2">Button2</button> <input type="text" id="myText" /> </form> 

How to make that when I hit enter in the Mytext value It's click on myButton2 ? 当我按下Mytext值时,如何使它进入单击myButton2?

DEMO: http://jsfiddle.net/WZ6TM/610/ 演示: http : //jsfiddle.net/WZ6TM/610/

UPDATE: 更新:

The following answers not working as expected, please see my code structure: 以下答案无法正常工作,请查看我的代码结构:

$(document).ready(function () {

    $('input#manualuser').keyup(function(e) {
        if (e.keyCode === 13) $('#search').click();
      });


    $('#search').click(function search() {
        if ($("input#manualuser").val() === '') {

            $('#infostatus').html("<label id='infostatus' class='info'>Please Enter Username</label>");

        }
        else {
            $.ajax({
                url: "https://" + $seldom + ":443/api/example/" + $("input#manualuser").val(),
                type: "GET",
                dataType: 'Jsonp',
                success: function (result) {....

Problem is being that the default action of pressing Enter key (keydown event) is 'activate' the focused item (actually button1). 问题是按Enter键(按下事件)的默认操作是“激活”焦点项目(实际上是button1)。 You need to prevent defaults. 您需要防止默认设置。 And then customize your events as follow. 然后如下自定义事件。

Here the snippet 这是片段

http://jsfiddle.net/WZ6TM/637/ http://jsfiddle.net/WZ6TM/637/

$(document).ready(function () { 

$('#myText').keydown (function(e) {
 if(e.which == 13) {  
e.preventDefault();
}
});

$('#myText').keyup(function (e) {
e.preventDefault();

  if(e.which == 13) {   
$("#myButton2").trigger('click');

}

});

$('#myButton').button().click(function () {
   alert('Button click 1!');
});

$('#myButton2').button().click(function () {
   alert('Button click 2!');
});
});

In the example if you close the dialog with Enter, event is launched again so take care and dont think that is doing a loop or something. 在示例中,如果您使用Enter关闭对话框,则事件将再次启动,因此请当心,不要以为正在循环或执行其他操作。 It is actually working and I hope you understand what is happening. 它实际上正在运行,希望您了解正在发生的事情。

Cheers 干杯

you need to catch the form submit 您需要赶上表格提交

$('form').submit(function(e){
  e.preventDefault();
  $('#mybutton2').click()
})

Alternatively, you could catch $('body').keyup() and do things with that. 或者,您可以捕获$('body')。keyup()并执行此操作。

I see two possibilities. 我看到两种可能性。 The simplest would be to hook off of the submit event: 最简单的方法是取消Submit事件:

$(document).ready(function () { 
  $('form').submit(function (e) {
   e.preventDefault();
   $('#myButton2').click();
  });

  $('#myButton').click(function () {
   alert('Button click 1!');
  });

  $('#myButton2').click(function () {
   alert('Button click 2!');
  });
});

And change the mark-up so that the buttons will not submit by default: 并更改标记,以便默认情况下按钮不会提交:

<form action="#">
<button type="button" id="myButton">Button</button>
<button type="button" id="myButton2">Button2</button>
<input type="text" id="myText" />
</form>

The other way is to use keydown and listen for the enter key. 另一种方法是使用keydown并侦听enter键。 I wouldn't suggest using this way, though, as listening for specific key codes can make your code harder to reason about. 不过,我不建议您使用这种方式,因为侦听特定的键控代码可能会使您的代码难以推理。

http://jsfiddle.net/WZ6TM/644/ Edit: Fiddle provided http://jsfiddle.net/WZ6TM/644/编辑:提供了Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM