同じ入力フィールドに複数の値を追加したい jquery オートコンプリート フィールドがあります。ここに表示されている例では、http://jqueryui.com/demos/autocomplete/#multiple値とラベルが同じであるため、select イベントの内容で問題なく動作します。ただし、私の場合、ユーザーが名前を選択すると、入力ボックスにラベルを表示したいのですが、フォームを送信するときに値を投稿したいと思います。それを回避するために、フォームに隠しフィールドを追加し、値を取得する別の配列を用意しましたが、ユーザーがバックスペースで値を戻したときに問題が発生しました。それはまだ私の配列にあります。それを修正する方法はありますか?またはおそらくより良い解決策ですか?
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 );
};