我正在使用自动完成的网络服务唱JSON,如果我选择的列表项不能再出现在自动完成列表中;
JSON AJAX代码:
select: function (event, ui) {
var terms = split(this.value);
if (terms.length <= 10) {
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
else {
var last = terms.pop();
$(this).val(this.value.substr(0, this.value.length - last.length - 0)); // removes text from input
$(this).effect("highlight", {}, 1000);
$(this).addClass("red");
$("#warnings").html("<span style='color:red;'>Max skill reached</span>");
return false;
}
}
答案 0 :(得分:1)
就像你对问题的评论中提到的@Bindred一样,更简单的解决方案是使用Select2 jQuery库。它并不完全是你想要的,但就UX而言,我认为它会达到类似的目标,而且工作变得轻而易举。
我添加了一个示例供您使用:https://jsfiddle.net/9cqc5876/9/
<强> HTML 强>
<select id="txtExpertise" multiple="multiple"></select>
<强> JavaSript 强>
$(document).ready(function() {
$("#txtExpertise").prop("disabled", "disabled");
// do your ajax request for data
//$.getJSON("../WebServices/WebServiceSkills.asmx/GetAutoCompleteData", function(data) {
// fake json data
var data = {"languages": ["Java", "C", "C++", "PHP", "Visual Basic",
"Python", "C#", "JavaScript", "Perl", "Ruby"]};
// populate the select
$.each(data.languages, function(key, val) {
$('#txtExpertise')
.append($("<option></option>")
.attr("value", key)
.text(val));
});
// activate the select2
$("#txtExpertise").select2();
$("#txtExpertise").prop("disabled", false);
//});
});