Jquery:如何在按下回车键时触发点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18160342/
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-26 21:08:18  来源:igfitidea点击:

Jquery: how to trigger click event on pressing enter key

jquerykey-events

提问by Arianule

I need to execute a button click event upon pressing key enter.

我需要按回车键执行按钮单击事件。

As it is at the moment the event is not firing.

就目前而言,该事件并未触发。

Please Help me with the syntax if possible.

如果可能,请帮助我使用语法。

$(document).on("click", "input[name='butAssignProd']", function () {
   //all the action
});

this is my attempt to fire click event on enter.

这是我在输入时触发点击事件的尝试。

$("#txtSearchProdAssign").keydown(function (e) {
  if (e.keyCode == 13) {
    $('input[name = butAssignProd]').click();
  }
});

回答by Vijay

try out this....

试试这个....

$('#txtSearchProdAssign').keypress(function (e) {
 var key = e.which;
 if(key == 13)  // the enter key code
  {
    $('input[name = butAssignProd]').click();
    return false;  
  }
});   

$(function() {

  $('input[name="butAssignProd"]').click(function() {
    alert('Hello...!');
  });

  //press enter on text area..

  $('#txtSearchProdAssign').keypress(function(e) {
    var key = e.which;
    if (key == 13) // the enter key code
    {
      $('input[name = butAssignProd]').click();
      return false;
    }
  });

});
<!DOCTYPE html>
<html>

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  <meta charset=utf-8 />
  <title>JS Bin</title>
</head>

<body>
  <textarea id="txtSearchProdAssign"></textarea>
  <input type="text" name="butAssignProd" placeholder="click here">
</body>

</html>



Find Demo in jsbin.com

在 jsbin.com 中找到 Demo

回答by Neeraj Dubey

Try This

尝试这个

$('#twitterSearch').keydown(function(event){ 
    var keyCode = (event.keyCode ? event.keyCode : event.which);   
    if (keyCode == 13) {
        $('#startSearch').trigger('click');
    }
});

Hope it helps you

希望对你有帮助

回答by geekchic

$('#txtSearchProdAssign').keypress(function (e) {
  if (e.which == 13) {
    $('input[name = butAssignProd]').click();
    return false;  
  }
});

I also just found Submitting a form on 'Enter'which covers most of the issues comprehensively.

我还刚刚发现在“Enter”上提交表单,它全面涵盖了大多数问题。

回答by Rohit416

You were almost there. Here is what you can try though.

你快到了。不过,您可以尝试以下方法。

$(function(){
  $("#txtSearchProdAssign").keyup(function (e) {
    if (e.which == 13) {
      $('input[name="butAssignProd"]').trigger('click');
    }
  });
});

I have used trigger()to execute click and bind it on the keyupevent insted of keydownbecause clickevent comprises of two events actually i.e. mousedownthen mouseup. So to resemble things same as possible with keydownand keyup.

我曾经trigger()执行 click 并将其绑定在keyup事件 insted 上,keydown因为click事件实际上包含两个事件,即mousedownthen mouseup。因此,要尽可能使用keydown和来相似事物keyup

Here is a Demo

这是一个演示

回答by Daniel Dewhurst

Another addition to make:

另一个补充:

If you're dynamically adding an input, for example using append(), you must use the jQuery on()function.

如果您要动态添加输入,例如使用append(),则必须使用 jQueryon()函数。

$('#parent').on('keydown', '#input', function (e) {
    var key = e.which;
    if(key == 13) {
        alert("enter");
        $('#button').click();
        return false;
    }
});

UPDATE:

更新:

An even more efficient way of doing this would be to use a switch statement. You may find it cleaner, too.

一种更有效的方法是使用 switch 语句。你也可能会发现它更干净。

Markup:

标记:

<div class="my-form">
  <input id="my-input" type="text">
</div>

jQuery:

jQuery:

$('.my-form').on('keydown', '#my-input', function (e) {
  var key = e.which;
  switch (key) {
  case 13: // enter
    alert('Enter key pressed.');
    break;
  default:
    break;
  }
});

回答by Shaun

Are you trying to mimic a click on a button when the enter key is pressed? If so you may need to use the triggersyntax.

当按下回车键时,您是否试图模仿对按钮的点击?如果是这样,您可能需要使用trigger语法。

Try changing

尝试改变

$('input[name = butAssignProd]').click();

to

$('input[name = butAssignProd]').trigger("click");

If this isn't the problem then try taking a second look at your key capture syntax by looking at the solutions in this post: jQuery Event Keypress: Which key was pressed?

如果这不是问题,那么尝试通过查看这篇文章中的解决方案来再次查看您的键捕获语法:jQuery Event Keypress:哪个键被按下?

回答by Arron

Just include preventDefault() function in the code,

只需在代码中包含 preventDefault() 函数,

$("#txtSearchProdAssign").keydown(function (e) 
{
   if (e.keyCode == 13) 
   {
       e.preventDefault();
       $('input[name = butAssignProd]').click();
   }
});

回答by Vishnu Prasad

Try This

尝试这个

<button class="click_on_enterkey" type="button" onclick="return false;">
<script>
$('.click_on_enterkey').on('keyup',function(event){
  if(event.keyCode == 13){
    $(this).click();
  }
});
<script>

回答by sandeep kumar

You can use this code

您可以使用此代码

$(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
       alert('You pressed a ENTER key.');
    }
});

回答by Supriya

     $('#Search').keyup(function(e)
        {
            if (event.keyCode === 13) {
            e.preventDefault();
            var searchtext = $('#Search').val();
            window.location.href = "searchData.php?Search=" + searchtext + '&bit=1';
            }
        });