最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
BootStrap select2 动态改变值的方法
时间:2022-06-25 18:07:28 编辑:袖梨 来源:一聚教程网
1,selec2动态赋值
代码如下 | 复制代码 |
vartemp=JSON.stringify({ id:"1|所有停车场", name:"所有停车场"}); $("#e_pid").attr("value", tempP); $("#e_pid").select2({ placeholder:"查找停车场名称(多选)", language:"zh-CN", minimumInputLength: 1, allowClear:true, multiple:true, ajax: { // instead of writing the function to execute the request we use Select2's convenient helper url:"/manage/park/index/json/index", dataType: 'json', data: function (term, page) { return { parkName: term,// search term powerpid: "1" }; }, results: function (data, page) { // parse the results into the format expected by Select2. // since we are using custom formatting functions we do not need to alter remote JSON data for (var i = 0; i < data.length; i++) { data[i].id = data[i].id+"|"+data[i].name; }; data.push({ id: "577cb125f8d2c404572413d1|无", name: "无" }); return { results: data }; } }, initSelection: function (element, callback) { //赋初始值 // the input tag has a value attribute preloaded that points to a preselected movie's id // this function resolves that id attribute to an object that select2 can render // using its formatResult renderer - that way the movie name is shown preselected vardata = []; varvalue ="" varstr = $(element).val().split('^'); for(vari = 0; i < str.length; i++) { vartemp = JSON.parse(str[i]); value += temp.id +","; data.push(temp); } ; value = value.substring(0, value.length - 1); $(element).val(value); callback(data); }, formatSelection:function(item) { returnitem.name;//注意此处的name,要和ajax返回数组的键值一样 },// 选择结果中的显示 formatResult:function(item) { returnitem.name;//注意此处的name },// 搜索列表中的显示 dropdownCssClass:"bigdrop",// apply css that makes the dropdown taller escapeMarkup:function(m) { returnm; } }); |
2,动态改变值
代码如下 | 复制代码 |
$("#e_pid").attr("value", temp); $("#e_pid").trigger('change');//动态改变值以后必须触发改变时间。否则将不会生效 |
相关文章
- “十月朝,糍粑碌碌烧”说的是小雪时节的哪一项习俗 蚂蚁庄园11月22日答案早知道 11-25
- 以闪亮之名宠物礼包怎么样 11-25
- 崩坏星穹铁道星期日用什么光锥 11-25
- 崩坏星穹铁道星期日用什么光锥 崩铁星期日光锥推荐搭配介绍 11-25
- 崩坏星穹铁道星期日技能机制怎么样 崩铁星期日技能机制介绍 11-25
- 崩坏星穹铁道星期日遗器怎么选择 崩铁星期日遗器推荐搭配介绍 11-25