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
Keypress event on chosen single select
提问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 keyup
event on the jquery chosen dropdown, after chosen in initialized.
将keyup
事件绑定在jquery 选择的下拉列表上,在选择后初始化。
Depending upon the version either you need to use .chosen-container
or .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...
}
});