当我在引导模式中使用select2 (input)时,我不能输入任何东西。就像残疾?在modal之外select2工作正常。

工作示例:http://jsfiddle.net/byJy8/1/ 代码:

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Panel</h3>
    </div>
    <div class="modal-body" style="max-height: 800px">          
      <form class="form-horizontal">
        <!-- Text input-->
        <div class="control-group">
            <label class="control-label" for="vdn_number">Numer</label>
            <div class="controls">
                <!-- seleect2 -->
                <input name="vdn_number" type="hidden" id="vdn_number"  class="input-large" required=""  />
            </div>
        </div>
      </form>    
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
    </div>
</div>

JS

$("#vdn_number").select2({
    placeholder: "00000",
    minimumInputLength: 2,
    ajax: {
        url: "getAjaxData/",
        dataType: 'json',
        type: "POST",
        data: function (term, page) {
            return {
                q: term, // search term
                col: 'vdn'
            };
        },
        results: function (data) { // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to alter remote JSON data
            return {results: data};
        }
    }
});

答案:

在这里你可以找到一个快速的解决方法

这里是“正确的方法”:Select2在嵌入引导模式时不起作用


当前回答

根据官方的select2文档,这个问题的发生是因为Bootstrap模态倾向于从模态之外的其他元素窃取焦点。

默认情况下,Select2将下拉菜单附加到元素上,它被认为是“在模式之外”。

相反,使用dropdownParent设置将下拉菜单附加到模式本身:

$('#myModal').select2({
   dropdownParent: $('#myModal')
});

参考资料:https://select2.org/troubleshooting/common-problems

其他回答

你不应该像大多数答案中建议的那样,将下拉框固定在情态动词上。 如果你的select2在模态的底部,你需要向下滚动来找到它,那么你就会有一个下拉的位置问题,因为你把它粘在了模态上。

相反,您应该将下拉父目录设置为输入父目录。在你的例子中,它应该是这样的

$(document).ready(function() {
  $("#vdn_number").select2({
    dropdownParent: $("#vdn_number").parent()
  });
});

我在github上找到了select2的解决方案

https://github.com/ivaynberg/select2/issues/1436

对于bootstrap 3,解决方案是:

$.fn.modal.Constructor.prototype.enforceFocus = function() {};

Bootstrap 4重命名了enforceFocus方法为_enforceFocus,所以你需要修补它:

$.fn.modal.Constructor.prototype._enforceFocus = function() {};

从以上链接复制的解释:

Bootstrap向focusin事件注册了一个监听器,它检查被聚焦的元素是覆盖本身还是它的后代——如果不是,它只是重新聚焦在覆盖上。随着select2下拉框被附加到正文,这有效地防止你在文本框中输入任何东西。 您可以通过覆盖在模态上注册事件的enforceFocus函数来快速修复这个问题

更改select2.css文件

z-index: 9998;
...
z-index: 9999;
...
z-index: 10000;

to

z-index: 10000;
...
z-index: 10001;
...
z-index: 10002;

为了更好地理解tabindex元素是如何工作的,完成公认的回答:

The tabindex global attribute is an integer indicating if the element can take input focus (is focusable), if it should participate to sequential keyboard navigation, and if so, at what position. It can take several values:   -a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;   -0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;   -a positive value means should be focusable and reachable via sequential keyboard navigation; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex. If several elements share the same tabindex, their relative order follows their relative position in the document.

来自:Mozilla开发人员网络

如果你的iPad键盘在点击select2输入时隐藏了引导模式,你可以通过在select2输入初始化后添加以下规则来解决这个问题:

if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
   var styleEl = document.createElement('style'), styleSheet;
   document.head.appendChild(styleEl);
   styleSheet = styleEl.sheet;
   styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);
   document.body.scrollTop = 0; // Only for Safari
}

摘自https://github.com/angular-ui/bootstrap/issues/1812#issuecomment-135119475

编辑:如果你的选项没有正确显示,你需要在初始化select2时使用dropdownParent属性:

$(".select2").select2({
    dropdownParent: $("#YOURMODALID")
});

祝你好运