success: function (data) { var oldnumber = new Array(); $.each(data, function (i) { oldnumber.push(data[i].id); }); $('#editcolor .selectpicker').selectpicker('val', oldnumber);//默认选中 $('#editcolor .selectpicker').selectpicker('refresh'); }上面是ajax获取默认值,记得selectpicker是通过id来操作的,
<div class="col-md-8" id="editcolor"> <select placeholder="请选择颜色" id="selectpickerallstr" name="selectpickerallstr" class="selectpicker show-tick form-control" data-style="btn-success" multiple data-live-search="true"> </select> </div>你们可以参考我前面的一篇文章,我再贴一遍获取代码
//select颜色 var loadTemplateColor = function () { $.ajax({ type: 'GET', url: root + '/mycolor', success: function (data) { var data = eval(data); $.each(data, function (i) { $("<option value='" + data[i].id + "'>" + data[i].text + "</option>") .appendTo(".selectpicker"); }); $('.selectpicker').selectpicker({ //我是对所有的selectpicker操作一次性赋值,如果你想单独赋值,好ok,那么就这样赋值: //appendTo("#editcolor .selectpicker"),就这样,在你select元素上面罩上一个div, //用div的id就可以标记你要操作的selectpicker,也就是你想操作的select元素标签了 style: 'btn-info', size: 8 }) } }); }
一次搞定一个值的赋值:
$('#editcolor .selectpicker').selectpicker('val', 1);//默认选中
$('#editcolor .selectpicker').selectpicker('refresh');一次搞定很多值的赋值:
success: function (data) { var oldnumber = new Array(); $.each(data, function (i) { oldnumber.push(data[i].id); }); $('#editcolor .selectpicker').selectpicker('val', oldnumber);//默认选中 $('#editcolor .selectpicker').selectpicker('refresh'); }代码就这么多, http://www.jq22.com/yanshi302这是文档全英文的,如果你们对我写的不认同可以一起讨论一下,交流会有收获的,(笔记)