我有一個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
我想你可以確定什麼應該在數組中,當該字段是「模糊」出來的,但也有一些問題,那也是 –