Javascript jQuery 事件按键:回车键

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

jQuery Event Keypress: Enter key

javascriptjqueryhtml

提问by Java Questions

i have a combo box which has got values and i want to give the user to select the values when the Enterkey pressed.

我有一个具有值的组合框,我想让用户在Enter按下键时选择值。

  1. User can navigate through Arrowkey
  2. Select the value when user enters Enterkey.
  1. 用户可以通过Arrow键导航
  2. 选择用户输入Enter密钥时的值。

I have done this one :

我做了这个:

$('#cmb_CIMtrek_DailyshipCo_CustomerName select').bind('keypress', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 13) { //Enter keycode
       //Do something
         alert("Enter key Pressed");
     }
});

but is not giving alert when I press Enterkey.

但是当我Enter按键时没有发出警报。

What could be the problem and how to solve it?

可能是什么问题以及如何解决它?

Best Regards.

此致。

回答by catherine

 <select>
    <option value="1">1</option>
    <option value="2">2</option>
 </select>     

 <script> 
 $('select').live('keypress',function(e){
     var p = e.which;
     if(p==13){
         alert('enter was pressed');
     }
 });
 </script>

回答by Varada

Try this one

试试这个

$('#cmb_CIMtrek_DailyshipCo_CustomerName select').keypress(function(event){

        var keycode = (event.keyCode ? event.keyCode : event.which);
        if(keycode == '13'){
            alert('You pressed a "enter" key in textbox');  
        }
        event.stopPropagation();
    });

回答by Rembunator

If you want to post the form when the user presses enter you could also use a submit button which has this behaviour as a default.

如果您想在用户按下回车键时发布表单,您还可以使用默认行为的提交按钮。

If you don't want to post the form but do have a submit button, this might catch the key event and doesn't propagate.So remove any submit-button.

如果您不想发布表单但确实有一个提交按钮,这可能会捕获关键事件并且不会传播。因此删除任何提交按钮。

To restrict the event to an object use:

要将事件限制为对象使用:

if (e.target == document.getElementById('element-id'))

or jquery

或 jquery

if (this == $('#element-id').get(0))

Your code would look something like this:

您的代码如下所示:

$(document).bind('keypress', function(e) 
    {
    // Use 'this' or 'e.target' (whithout quotes)
    if (this == $('#cmb_CIMtrek_DailyshipCo_CustomerName select').get(0))
        {
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13)
            { //Enter keycode
            //Do something
            alert("Enter key Pressed");
            }
        }
    // Stop the event from triggering anything else
    e.stopPropagation();
    });

回答by axel ajin

For example:

例如:

<!html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<div class="form-group">
    <label for="exampleFormControlSelect1">Bodega</label>
    <select class="form-control" id="exampleFormControlSelect1">
      <option value="despacho">Despacho</option>
      <option value="ventas">Ventas</option>
    </select>
</div>

 <script>
     $('#exampleFormControlSelect1').on('keypress',function(e){
         var p = e.which;
         if(p==13){
             alert('enter was pressed');
         }
     });
 </script>