我有一个具有以下值的输入:e22,e33,e35
在那个输入上,我想select2
用上面提到的默认值进行初始化。我用来尝试这样做的代码如下:
$('input[name="postal_code"]').select2({
placeholder: 'search a postal code',
allowClear: true,
minimumInputLength: 2,
maximumSelectionSize: $maxInput,
multiple: true,
tokenSeparators: [",", " "],
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term)===0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
ajax: {
url: $url,
dataType: "json",
data: function(term, page) {
return {
query: term
};
},
results: function(data, page) {
console.log(data);
return {
results: data,
};
}
},
initSelection: function(element, callback) {
var data = [];
$(element.val().split(",")).each(function(i, v) {
console.log(v);
data.push({
text: v
});
});
console.log(data);
callback(data);
}
});
问题是输入,在选择初始化之后,而不是具有以下标签:e22,e33,e35;我只有e22
任何想法,为什么其余的被切断?
JSFiddle:http: //jsfiddle.net/alleks/fMXxL/