當前位置:才華齋>計算機>java語言>

BootStrap select2 如何實現動態改變值

java語言 閱讀(2.23W)

導語:BootStrap select2 如何實現動態改變值呢?下面小編給大家提供了程式碼實現教程,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。

BootStrap select2 如何實現動態改變值

  1,selec2動態賦值

var temp=JSON.stringify({ id: "1|所有停車場", name: "所有停車場" });

$("#e_pid")("value", tempP);

$("#e_pid")ct2({

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 < th; i++) {

data[i] = data[i]+"|"+data[i];

};

({ 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)()t('^');

for (var i = 0; i < th; i++) {

var temp = e(str[i]);

value += + ",";

(temp);

}

;

value = tring(0, th - 1);

$(element)(value);

callback(data);

},

formatSelection: function (item) {

return ;//注意此處的name,要和ajax返回陣列的鍵值一樣

}, // 選擇結果中的顯示

formatResult: function (item) {

return ;//注意此處的name

},// 搜尋列表中的顯示

dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller

escapeMarkup: function (m) {

return m;

}

});

  2,動態改變值

$("#e_pid")("value", temp);

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