http://www.cnblogs.com/wuhuacong/p/4761637.html
2.这个做详细参考
http://www.jianshu.com/p/c5ab74b91b2e
3、http://m.blog.csdn.net/u011317027/article/details/62890088
4、http://www.cnblogs.com/liuxiaobo93/p/5112993.html
5、方法api
http://a714115852.iteye.com/blog/2097033
6.方法和api
http://www.cnblogs.com/liuxiaobo93/p/5112993.html
PS: 这是三种select2调通的代码
7. 参考
http://www.51xuediannao.com/js/jquery/select2.html
8.图片修改select2
https://codepen.io/ryanjgill/pen/gcmrH
<!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"> <!-- Bootstrap 3.3.5 --> <link rel="stylesheet" href="css/bootstrap.min.css"> <!-- select2 style --> <link rel="stylesheet" href="css/select2.min.css"> </head> <body class="hold-transition skin-blue sidebar-mini" style="background-color:#ecf0f5;"> <input type="button" onclick="test()" value="测试"/> <div> <input type="text" id="i1"/> </div> <!-- Main content --> <section class="content container" style="margin-top: 15px;"> <div class="col-sm-4"> <h3>本地数据方式</h3> <select multiple="multiple" class="form-control input-sm downList1"> <option></option> </select> <p class="p1"></p> <hr> <button type="button" class="btn btn-sm btn-primary" id="one">click</button> </div> <div class="col-sm-4"> <h3>AJAX获取数据方式,只请求一次</h3> <select class="form-control input-sm downList3" id="hahah" multiple="multiple"> <option></option> </select> <p class="p2"></p> <hr> <button type="button" class="btn btn-sm btn-primary" id="two">click</button> </div> <div class="col-sm-4"> <h3>AJAX获取数据方式</h3> <select multiple="multiple" class="form-control input-sm downList2" id="xxx"> <option></option> </select> <p class="p2"></p> <hr> <button type="button" class="btn btn-sm btn-primary" id="two">click</button> </div> </section><!-- /.content --> <script src="js/jquery-2.0.3.min.js"></script> <script src="js/select2.full.js"></script> <script src="js/zh-CN.js"></script> </body> <script> var oneReq = []; $("#i1").on("focusout",function(){ alert("f"); }); function test(){ } $(function(){ $.ajax({ type:"get", url:"http://127.0.0.1:8020/Test/test.json", dataType:"json", asynchronous:true, contentType:"application/json", success:function(data){ //alert("success"); var data1 = $(data.items); var result = []; $.each(data1,function(infoIndex,info){ if(info.business == "" && info.owner == "") { result.push({id: infoIndex, text: info.name}); } else{ //$("#e2").addNewTag("444"); result.push({id: infoIndex, text: info.name+ ":"+ info.owner + "," + info.business}); } }) oneReq = result; $("#hahah").select2({ data: oneReq, placeholder:'请选择',//默认文字提示 language: "zh-CN",//汉化 allowClear: true//允许清空 }) //alert($(oneReq).size()); }, error:function(data){ alert("lose"); } }); }) //one 本地数据方式 var dataList = [ { id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' } ]; //var str = "{'id':'0','text':'bug'}"; //var obj = JSON.parse(str); // --> parse error //var json1 = eval('(' + str + ')'); //alert(json1.t); $(".downList1").select2({ data: dataList, placeholder:'请选择',//默认文字提示 language: "zh-CN", allowClear: true//允许清空 }) $('#one').click(function(){ var id = $(".downList1").select2("data")[0].id; var text = $(".downList1").select2("data")[0].text; var html = '您选择了:' + '{ id:' + id + ', ' + 'text: ' + text + ' }'; $('.p1').text(html); }) // alert($(oneReq).size()); //two AJAX获取数据方式 $("#xxx").select2({ ajax: { type:"get", url: "http://127.0.0.1:8020/Test/test.json", dataType: 'json', delay: 250, data: function (params) { return { q: params.term, // search term 请求参数 page: params.page }; }, processResults: function (data, params) { params.page = params.page || 1; var itemList = []; //alert("xxxxxxxx"); console.log(data); var data1 = $(data.items); $.each(data1,function(infoIndex,info){ if(info.business == "" && info.owner == "") { itemList.push({id: infoIndex, text: info.name}); } else{ //$("#e2").addNewTag("444"); itemList.push({id: infoIndex, text: info.name+ ":"+ info.owner + "," + info.business}); } }) /*for(var i=0; i<data1.size(); i++){ if(data1.business == "" && data1.owner == "") { itemList.push({id: i, text: $(data1.name)}); //$("<option value="+info.name+">"+info.name+"</option>").appendTo($("#e2")); } else{ //$("#e2").addNewTag("444"); itemList.push({id: i, text: data1.name+ ":"+ data1.owner + "," + data1.business}); //$("<option value="+info.name+">"+info.name + ":"+ info.owner + "," + info.business+"</option>").appendTo($("#e2")); } }*/ return { results: itemList, //data.items, pagination: { more: (params.page * 30) < data.total_count } }; }, cache: true }, placeholder:'请选择',//默认文字提示 language: "zh-CN", // tags: true,//允许手动添加 // allowClear: true,//允许清空 escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入 // minimumInputLength: 1, // formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果 // formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择 }); $('#two').click(function(){ var id = $(".downList2").select2("data")[0].id; var text = $(".downList2").select2("data")[0].text; var html = '您选择了:' + '{ id:' + id + ', ' + 'text: ' + text + ' }'; $('.p2').text(html); }) </script> </html>