我有一个jquery自动填充字段,我想在同一个输入字段中添加多个值。在我看到的例子中,http://jqueryui.com/demos/autocomplete/#multiple的值和标签是一样的,所以事情在选择事件中可以正常工作。然而,就我而言,当用户选择一个名称时,我希望输入框显示标签,但我想在提交表单时发布这些值。为了解决这个问题,我在表单中添加了一个隐藏字段,并且有一个单独的数组来获取值,但是我的问题是当用户退出该值时。它仍然在我的阵列。任何想法如何解决这个问题?或者更好的解决方案?jquery自动完成。跟踪值而不是标签
var friend_data = [{"label":"JOhn Doe","value":"12345"},{"label":"Jane Doe","value":"123456"}];
$("#friends")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
//source: friend_data,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
friend_data, extractLast(request.term)));
},
delay: 0,
autofocus: true,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.label);
friend_id_list.push(ui.item.value);
console.log(friend_id_list);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
console.log(this.value);
return false;
}
})
.data("autocomplete")._renderItem = function(ul, item) {
var fb_pic_path = '<img src="http://graph.facebook.com/' + item.value + '/picture?type=square">' ;
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + fb_pic_path + item.label + "</a>")
.appendTo(ul);
};
感谢您的格式! – jtwg
我看上去很高,但是我没有看到其他人有类似的问题。奇怪的。也许我应该只是检查每个键盘上的输入字段值(带有标签列表),并且如果该列表中的逗号数量不匹配'id'数组长度,只弹出最后一个数字?任何想法,如果这将被视为一个很好的解决方案? – jtwg
我想你可以确定什么应该在数组中,当该字段是“模糊”出来的,但也有一些问题,那也是 –