BootStrap select2 動(dòng)態(tài)改變值的方法
1,selec2動(dòng)態(tài)賦值
var temp=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 var data = []; var value = "" var str = $(element).val().split('^'); for (var i = 0; i < str.length; i++) { var temp = 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) { return item.name;//注意此處的name,要和ajax返回?cái)?shù)組的鍵值一樣 }, // 選擇結(jié)果中的顯示 formatResult: function (item) { return item.name;//注意此處的name },// 搜索列表中的顯示 dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller escapeMarkup: function (m) { return m; } });
2,動(dòng)態(tài)改變值
$("#e_pid").attr("value", temp); $("#e_pid").trigger('change'); //動(dòng)態(tài)改變值以后必須觸發(fā)改變時(shí)間。否則將不會生效
以上所述是小編給大家介紹的BootStrap select2 動(dòng)態(tài)改變值的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- BootStrap的select2既可以查詢又可以輸入的實(shí)現(xiàn)代碼
- JS組件Bootstrap Select2使用方法解析
- 基于BootStrap Metronic開發(fā)框架經(jīng)驗(yàn)小結(jié)【三】下拉列表Select2插件的使用
- JS組件Bootstrap Select2使用方法詳解
- bootstrap模態(tài)框遠(yuǎn)程示例代碼分享
- 擴(kuò)展bootstrap的modal模態(tài)框-動(dòng)態(tài)添加modal框-彈出多個(gè)modal框
- BootStrap模態(tài)框和select2合用時(shí)input無法獲取焦點(diǎn)的解決方法
相關(guān)文章
一文詳解JavaScript中的URL和URLSearchParams
URL,稱為統(tǒng)一資源定位器,指互聯(lián)網(wǎng)上能找到資源定位的字符串,而URLSearchParams對象是專門用于處理url網(wǎng)址信息中的查詢字符串,本文就來帶大家深入了解一下二者的使用2023-05-05實(shí)例分析JS與Node.js中的事件循環(huán)
本篇文章通過實(shí)例給大家詳細(xì)分析了JS與Node.js中的事件的原理以及用法,大家學(xué)習(xí)一下吧。2017-12-12微信小程序手機(jī)號授權(quán)一鍵登錄功能實(shí)現(xiàn)代碼
在微信小程序中實(shí)現(xiàn)手機(jī)號一鍵登錄功能,首先需要通過uni.login調(diào)用獲取微信的登錄憑證(code),然后將此code發(fā)送到服務(wù)端,本文給大家介紹微信小程序手機(jī)號授權(quán)一鍵登錄功能,感興趣的朋友跟隨小編一起看看吧2024-10-10javascript實(shí)現(xiàn)密碼驗(yàn)證
這篇文章主要介紹了javascript密碼驗(yàn)證的實(shí)現(xiàn)方法,過程很簡單具有一定的參考價(jià)值,適合初學(xué)者學(xué)習(xí)研究,感興趣的小伙伴們可以參考一下2015-11-11關(guān)于JS判斷圖片是否加載完成且獲取圖片寬度的方法
本篇文章小編為大家介紹,關(guān)于JS判斷圖片是否加載完成且獲取圖片寬度的方法,有需要的朋友可以參考一下2013-04-04