Keypress event on chosen single select

陌路散爱 提交于 2019-12-11 08:32:51

问题


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>

回答1:


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

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



回答2:


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

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


来源:https://stackoverflow.com/questions/21106161/keypress-event-on-chosen-single-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!