我試圖只允許select2庫中的一個值,不管它是如何寫入的。例如,如果值「Test」在數據列表中,則不應再添加「test」。我搜索了一段時間,也查看了文檔,但我沒有解決這個問題。select2:禁用區分大小寫的匹配
$("#timezones").select2({
tags: true,
createTag: function (tag) {
return {
id: tag.term,
text: tag.term + " (new)",
isNew: true
};
},
matcher: function (term, data) {
// `term.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
if ($.trim(term.term) === '') {
return data;
}
var termString = term.term.trim();
var textString = data.text.trim();
var termStringUpper;
var textStringUpper;
if (termString) termStringUpper = termString.toUpperCase();
if (textString) textStringUpper = textString.toUpperCase();
return termStringUpper == textStringUpper;
}
});
這裏是一個的jsfiddle:https://jsfiddle.net/2sz0oj8m/
我可能還要注意,默認情況下,只有當'createTag'中返回的'id'與任何res中的'id'都不匹配時纔會顯示標籤ULTS。 –
謝謝,這幫了我很多! @凱文布朗:一個很好的提示,我會牢記這一點。 – baris1892