我正在开发一个表单,并使用jQuery UI自动完成.当用户 Select 一个选项时,我希望该选项弹出到附加到parent <p>
标记的范围中.然后,我希望字段被清除,而不是被 Select 填充.
我的跨度看起来很好,但我无法让场地清理干净.
如何取消jQuery UI自动完成的默认 Select 操作?
这是我的代码:
var availableTags = ["cheese", "milk", "dairy", "meat", "vegetables", "fruit", "grains"];
$("[id^=item-tag-]").autocomplete({
source: availableTags,
select: function(){
var newTag = $(this).val();
$(this).val("");
$(this).parent().append("<span>" + newTag + "<a href=\"#\">[x]</a> </span>");
}
});
Simply doing $(this).val("");
doesn't work. What is maddening is that almost the exact function works fine if I ignore autocomplete, and just take action when the user types a comma as such:
$('[id^=item-tag-]').keyup(function(e) {
if(e.keyCode == 188) {
var newTag = $(this).val().slice(0,-1);
$(this).val('');
$(this).parent().append("<span>" + newTag + "<a href=\"#\">[x]</a> </span>");
}
});
The real end result is to get autocomplete to work with multiple selections. If anybody has any suggestions for that, they would be welcome.