javascript 选定的单个选择上的按键事件

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

Keypress event on chosen single select

javascriptjqueryjquery-chosen

提问by Santosh Ghimire

I would like to automatically add the selected value when enter key is pressed on chosen jquery single select. So, for that is there any event like keypress which I would use to do something when return key was pressed?

我想在选定的 jquery 单选上按下 Enter 键时自动添加选定的值。那么,为此有没有像 keypress 这样的事件,当按下返回键时我会用它来做某事?

<select id="myselect" data-placeholder="Add foods you can buy here." 
style="height:30px; width: 100%" class="chosen-select" onkeypress="handle(event)" >
<option value=""></option>
<optgroup label="blah">
<option>blah blah</option>
</optgroup>
</select>

回答by Mahesh Sapkal

Bind the keyupevent on the jquery chosen dropdown, after chosen in initialized.

keyup事件绑定在jquery 选择的下拉列表上,在选择后初始化。

Depending upon the version either you need to use .chosen-containeror .chzn-container.

根据您需要使用的版本.chosen-container.chzn-container.

$(".chosen-select").chosen({});

$(".chosen-container").bind('keyup',function(e) {
    if(e.which === 13) {
        $('#myform').submit();
        // or your stuff here...
    }
});

回答by Emaborsa

I had problema by using the code suggesetd by Mahesh. If so, use keypress instead:

我在使用由Mahesh. 如果是这样,请改用按键:

$(".chosen-container").bind('keypress',function(e) {
    if(e.which === 13) {
        $('#myform').submit();
        // or your stuff here...
    }
});