我正在尝试实现一个标签输入框,但我希望限制用户只能从自动完成框中选择值。我试图使用源json覆盖beforeTagAdded事件,并检查标签是否存在于源属性中,但没有成功。
以下是代码,请查看beforeTagAdded函数。
$(document).ready(function () {
var itemId;
var theTags = $('#msg_to');
theTags.tagit({
autocomplete: {
source: [{ id: "1", value: 'David'}, { id: "2", value: 'John' }],
minLength: 0,
select: function (event, ui) {
itemId = ui.item.id;
theTags.tagit("createTag", ui.item.value);
}
},
showAutocompleteOnFocus: true,
afterTagAdded: function (event, ui) {
if (itemId) {
$(ui.tag).find('input').attr('name', "tag[\'" + itemId + "']['" + ui.tagLabel + "']");
itemId = null;
}
},
beforeTagAdded: function (event, ui) {
var id = ui.autocomplete.source; // not working
// what to do next?
}
})
});
</script>
提前感谢您的帮助。