欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BootStrap select2 动态改变值的方法

程序员文章站 2022-05-15 23:19:20
1,selec2动态赋值 var temp=json.stringify({ id: "1|所有停车场", name: "所有停车场" }); $("#e_pi...

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 动态改变值的方法,希望对大家有所帮助