弹出窗口中的select2 jquery插件?

时间:2016-01-20 07:40:32

标签: javascript jquery html

我有两个select代码工具select2 jquery plugin

  • select标记正常:工作正常,可以搜索。

  • 弹出窗口中的
  • select标签:无法搜索。



<head>
    <link rel="stylesheet" href="https://almsaeedstudio.com/themes/AdminLTE/plugins/select2/select2.min.css">
    <link rel="stylesheet" href="https://almsaeedstudio.com/themes/AdminLTE/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://almsaeedstudio.com/themes/AdminLTE/dist/css/AdminLTE.min.css">
    <script src='https://almsaeedstudio.com/js/bootstrap.min.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/dist/js/demo.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/plugins/select2/select2.full.min.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/dist/js/app.min.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/bootstrap/js/bootstrap.min.js'></script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</head>
<body>
<div>
    <select id="categoryID" onchange="changeCategory(this)" class="form-control select2" style="width: 100%">
        <option id="1">Games</option>
        <option id="2">Sport</option>
        <option id="3">Sport Games</option>
    </select>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/dist/js/demo.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/plugins/select2/select2.full.min.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/dist/js/app.min.js'></script>
    <script src='https://almsaeedstudio.com/themes/AdminLTE/bootstrap/js/bootstrap.min.js'></script>

    <button class="btn btn-block btn-primary" data-toggle="modal"
            data-target="#addNewCategoryModal">New
    </button>
    <div class="modal fade" id="addNewCategoryModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="titleLabel">New Category</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <select id="categoryID" onchange="changeCategory(this)" class="form-control select2" style="width: 100%">
                            <option id="1">Games</option>
                            <option id="2">Sport</option>
                            <option id="3">Sport Games</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function initialSelect() {
            $(".select2").select2();
        }
        $(function () {
            //Initialize Select2 Elements
            $(".select2").select2();
        });
        $('#addNewCategoryModal').on('show.bs.modal', function (event) {
            var modal = $(this)
            modal.find('.modal-body #categoryID').select2();
            modal.find('.modal-body categoryID').select2();
            modal.find('.select2').select2();
            $(".select2").select2();
        })
    </script>

</div>
</body>
&#13;
&#13;
&#13;

任何帮助。感谢。

2 个答案:

答案 0 :(得分:1)

您已为模态提供tabindex="-1"。因为这个输入没有得到关注。从代码中删除tabindex它应该可以工作。

您的代码中还存在以下问题

  • 您已经多次将HTML包含在bootstrap和select2 js文件中。仅包括一次。
  • 在包含bootstrap js文件之前包含您的jquery文件。
  • 您的HTML页面多次使用categoryID。 ID必须是唯一的。

答案 1 :(得分:0)

这对我有用

    <script>
        $('#mySelect2').select2({
            dropdownParent: $('#myModal')
        });
    </script>

来源:

Select2 web