SELECT2 -> Add data without replacing content

我是研究僧i 提交于 2019-11-30 21:36:35

You should be able to do that with simple:

var test = $('#test');

$(test).select2({
    data:[
        {id:0,text:"enhancement"},
        {id:1,text:"bug"},
        {id:2,text:"duplicate"},
        {id:3,text:"invalid"},
        {id:4,text:"wontfix"}
    ],
    multiple: true,
    width: "300px"
});
var data = $(test).select2('data');
data.push({id:5,text:"fixed"});
$(test).select2("data", data, true); // true means that select2 should be refreshed

Working example: http://jsfiddle.net/z96Ca/

You are replacing the value with val(mystuff). You want to do the following:

  1. Get the current data in the input with val()

    var dataOld = $('#selectPretty').val();

  2. Append the new data with something like*

    var dataNew = dataOld.push(value);

  3. Set the input data to new data:

    $('#selectPretty').val(dataNew);

*This assumes that val() returns an array

Docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!