使用jQuery和Select2,“Keypress”事件不能正常工作

时间:2021-03-15 20:33:49

I have a forum in which I change the functionality of tab to enter. When the user presses enter the next input field get focus and somehow iImanage to open the select2 select box on focusin event so the select 2 box opens up.

我有一个论坛,我在其中更改了tab的功能以进入。当用户按下进入下一个输入字段获取焦点时,以某种方式iImanage在focusin事件上打开select2选择框,这样select2框就会打开。

But, when I select the value and press enter in selec2 select box it does not close and remain open on enter key press event but it closes. When I change the enter key event to any keypress event than select2 get close and works fine but I have to do that by enter.

但是,当我在selec2选择框中选择值并按enter时,它不会关闭并在enter key press事件中保持打开,但它会关闭。当我将enter key事件更改为任何按键事件而不是select2时,关闭并正常工作,但是我必须通过enter进行操作。

What I want:
When the user selects the value in select2 search-box and presses enter it get close and focus move to next input field or any field.

我想要的:当用户选择select2搜索框中的值并按下时,输入它,并将焦点移到下一个输入字段或任何字段。

What i have done so far.

到目前为止我所做的一切。

$('.search-select').on("focusin",function(event) {
    $('#s2id_form-field-select-3').select2('open');
    event.preventDefault();  
});

Select2 with enter key event which isn't working

选择2并输入无效的键事件

$('.select2-input').on("keypress", function(e) {
            if (e.keyCode == 13) {
                $("#select2-drop-mask").click();
                $('#name').focus();
                e.preventDefault();   
                }
       });

Select2 with any keypress event which is working

使用正在工作的任何按键事件选择2

$('.select2-input').on("keypress", function(event) {
                $("#select2-drop-mask").click();
                $('#name').focus();
                event.preventDefault();         
});

Markup

标记

 <select id="form-field-select-3" name="register_id" class="form-control search-select" required="required">
   <option value="">&nbsp;</option>
   <? $query=$this->dba->get_dropdown('register',array('id','name'));
      foreach($query as $key=>$value):
   ?>
   <option value="<?=$key?>"><?=$value; ?></option>}
  <? endforeach;?>
</select>

Apologies in advance for mistakes, this is my first time using Select2.

事先为错误道歉,这是我第一次使用Select2。

1 个解决方案

#1


4  

I think you're looking for keydown:

我想你在找keydown:

$('.select2-input').on("keydown", function(e) {
            if (e.keyCode == 13) {
                $("#select2-drop-mask").click();
                $('#name').focus();
                e.preventDefault();   
                }
       });

They're a little different. In this case, keypress isn't inserting anything into your field.

他们有些不同。在本例中,keypress没有向字段插入任何内容。

#1


4  

I think you're looking for keydown:

我想你在找keydown:

$('.select2-input').on("keydown", function(e) {
            if (e.keyCode == 13) {
                $("#select2-drop-mask").click();
                $('#name').focus();
                e.preventDefault();   
                }
       });

They're a little different. In this case, keypress isn't inserting anything into your field.

他们有些不同。在本例中,keypress没有向字段插入任何内容。