所选单选上的按键事件

问题描述:

我想在选择的jquery单选上按下回车键时自动添加选择的值.那么,是否存在像按键之类的事件,当按下回车键时,我会用它来做某事?

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?

<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>

在初始化后将keyup事件绑定到选定的jquery下拉列表上.

Bind the keyup event on the jquery chosen dropdown, after chosen in initialized.

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

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

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

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