“按键"使用jQuery&事件无法正常工作选择2 [英] "Keypress" event is not working correctly, using jQuery & Select2

查看:94
本文介绍了“按键"使用jQuery&事件无法正常工作选择2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个论坛,可以在其中更改要输入的选项卡的功能.当用户按下输入时,下一个输入字段将获得焦点,并以某种方式iImanage打开focusin事件上的select2选择框,从而打开select 2框.

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.

但是,当我选择该值并在selec2 select框中按Enter时,它不会关闭,并在输入按键事件时保持打开状态,但会关闭.当我将enter键事件更改为select2以外的任何按键事件时,关闭它并可以正常工作,但是我必须通过enter来做到这一点.

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.

我想要什么:
当用户在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.

到目前为止我做了什么.

What i have done so far.

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

Select2的Enter键事件不起作用

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

Select2以及任何有效的按键事件

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

标记

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

事先为错误表示歉意,这是我第一次使用Select2.

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

推荐答案

我认为您正在寻找 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.

这篇关于“按键"使用jQuery&amp;事件无法正常工作选择2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆