溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

BootStrap select2 動(dòng)態(tài)改變值的方法

發(fā)布時(shí)間:2020-08-25 20:36:09 來(lái)源:腳本之家 閱讀:156 作者:zhudiwoaini 欄目:web開發(fā)

1,selec2動(dòng)態(tài)賦值

var temp=JSON.stringify({ id: "1|所有停車場(chǎng)", name: "所有停車場(chǎng)" });
$("#e_pid").attr("value", tempP);
              $("#e_pid").select2({
                placeholder: "查找停車場(chǎng)名稱(多選)",
                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|無(wú)", name: "無(wú)" });
                    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í)間。否則將不會(huì)生效

以上所述是小編給大家介紹的BootStrap select2 動(dòng)態(tài)改變值的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI