选择单选的Keypress事件

时间:2022-05-05 00:11:01

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单选时按下回车键时自动添加所选值。那么,为什么会出现像按键这样的事件,当按下返回键时我会用它做什么?

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

2 个解决方案

#1


10  

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

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

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...
    }
});

#2


0  

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

我通过使用Mahesh的代码suggesetd来解决问题。如果是这样,请使用按键代替:

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

#1


10  

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

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

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...
    }
});

#2


0  

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

我通过使用Mahesh的代码suggesetd来解决问题。如果是这样,请使用按键代替:

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