json数据“a.json”
[
{
"id": "1",
"text": "张三"
},
{
"id": "2",
"text": "李师师"
},
{
"id": "3",
"text": "林冲"
},
{
"id": "4",
"text": "周通"
},
{
"id": "5",
"text": "杨康"
}
]
js获取json文件数据
$.getJSON(url, function (data) {
control.empty();//清空下拉框
$.each(data, function (i, item) {
//alert(item.id)
control.append("<option value='" + item.id + "'> " + item.text + "</option>");
});
});
html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>select2</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Select2 -->
<link rel="stylesheet" href="plugins/select2/select2.min.css">
</head>
<body> <select class="js" multiple="multiple">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
<!-- jQuery 2.1.4 -->
<script src="http://cdn.hcharts.cn/jquery/jquery-2.1.4.min.js"></script>
<!-- Select2 -->
<script src="plugins/select2/select2.full.min.js"></script>
<!-- SlimScroll 1.3.0 -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- page script -->
<script src="js/data.js"></script> <script>
$(function () {
$(".js").select2();
BindDictItem('js','a');
});
</script>
</body>
</html>