BootStrap select2 動態改變值的方法,bootstrapselect2

來源:互聯網
上載者:User

BootStrap select2 動態改變值的方法,bootstrapselect2

1,selec2動態賦值

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返回數組的索引值一樣                }, // 選擇結果中的顯示                formatResult: function (item) {                  return item.name;//注意此處的name                },// 搜尋列表中的顯示                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller                escapeMarkup: function (m) {                  return m;                }              });

2,動態改變值                                             

$("#e_pid").attr("value", temp);                          $("#e_pid").trigger('change'); //動態改變值以後必須觸發改變時間。否則將不會生效

以上所述是小編給大家介紹的BootStrap select2 動態改變值的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.