Bootstrap3插件系列:bootstrap-select2
1.下载插件
https://github.com/select2/select2
2.引用插件
<script src="~/Scripts/jquery-1.12.4.js"></script> <link href="~/Content/bootstrap.css" rel="stylesheet" /> <script src="~/Scripts/bootstrap.js"></script>
<link href="~/Content/bootstrap-select2.min.css" rel="stylesheet" /> <script src="~/Scripts/bootstrap-select2.min.js"></script> <script src="~/Scripts/bootstrap-select2-zh-CN.js"></script>
3.下拉框初始化
示例代码:
<select class="bootstrap-select form-control"> <option value="110000">北京市</option> <option value="120000">天津市</option> <option value="130000">河北省</option> <option value="140000">山西省</option> <option value="150000">内蒙古自治区</option> </select>
<script type="text/javascript"> $(function () { $(".bootstrap-select").select2(); }); </script>
运行效果:
4. 数组数据初始化
示例代码:
<select class="bootstrap-select form-control"></select> <script type="text/javascript"> $(function () { var data = [ { "id": "110000", "text": "北京市" }, { "id": "120000", "text": "天津市" }, { "id": "130000", "text": "河北省" }, { "id": "140000", "text": "山西省" }, { "id": "150000", "text": "内蒙古自治区" } ] $(".bootstrap-select").select2({ data: data }); }); </script>
运行效果: