selectpicker下拉多选框ajax异步或者提前赋值=》默认值
Bootstrap select多选下拉框赋值
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'); }
动态赋值
<select id="filetype" name="filetype" class="selectpicker show-tick dropup" data-live-search="true" onchange="filetypeChange();"></select>
初始化调用 getdata4select("filetype","FILE_TYPE");
/**
* 根据字典类型查询字典数据,供bootstrap select使用
* @param id
* @param dicttype
*/
function getdata4select(id,dicttype){
var url ='/yh/com/dominsoft/act/YHTypeHandleAct/getItemType.act?type='+dicttype;
var json = getJsonRs(url);
if(json.rtState == "0"){
var rtData = json.rtData;
var typeData = rtData.typeData;
jQuery('#'+id).append("<option value='' >请选择</option>");
for (var i = 0; i < typeData.length; i++) {
jQuery('#'+id).append("<option value=" + typeData[i].typeId + " >" + typeData[i].typeDesc + "</option>");
}
}
jQuery('#'+id).selectpicker();
}