首先添加https://select2.github.io/dist/css/select2.min.css

和https://select2.github.io/dist/js/select2.full.js


再select变现添加 js-example-basic-single class。

在添加这个srcipt

<script type="text/javascript">
    $(document).ready(function() {
        $(".js-example-basic-single").select2();
    });
</script>
则形成一个可以搜索的select。

使用select2的几个问题及解决。
一、要使select选中某个元素,要使select选中four,$("#ddd").val("four");不能起作用,

<select id="ddd">
  <option value="one">First</option>
  <option value="two">Second (disabled)</option>
  <option value="three">Third</option>
  <option value="four">four1111111</option>
  <option value="five">five2222222222</option>
  <option value="six">six66666666</option>
</select>

可以使用如下方法:
var $ddd = $("#ddd").select2();

$ddd.val("four").trigger("change");

二、在bootstrap的模态框里使用select2插件,会导致select2里的input输入框没有办法获得焦点,没有办法输入.

解决方法:

1、

把页面中的  tabindex="-1"  删掉:

<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">
<div id="myModal" class="modal hide fade" 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">

2.

重写enforceFocus方法:

$.fn.modal.Constructor.prototype.enforceFocus = function() {};
三、用了select2,则select的不能执行onclick,监听方法。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐