【发布时间】:2014-11-30 15:55:02
【问题描述】:
我想使用autocomplete()-plugin 选择多个值。我确实从远程 JSON 文件收到的值。到目前为止,我可以获得 1 个值,但之后它失败了。我的目标是选择多个值并存储 ID,以便稍后发布这些值。
到目前为止,我有:
<input type="text" id="featured" autocomplete="on"></input>
<input type="hidden" value="" name="artist_id">
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$("#featured").bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}}).autocomplete({
minLength: 0,
source: function( request, response ) {
$.ajax({
url: App.APIO+'/i/search/artist?name='+request.term+'&featured=true',
dataType: "json",
success: function(data) {
var result = $.map(data.data, function(artist) {
return {value: artist.name, label: artist.name, artist_id: artist.artist_id};
});
response( result );
}
});
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var item = split( this.value );
// remove the current input
item.pop();
// add the selected item
item.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
item.push( "" );
this.value = item.join( ", " );
return false;
$('[name="artist_id"]').val(ui.item.artist_id); // somehow insert the artist_id here
}
});
现在我只能输入 1 个值并选择它,例如显示“Lady Gaga”,但之后我无法输入任何内容,例如没有任何反应。我可以在我的网络选项卡中看到它尝试加载
/artist?name=ladygaga%20%b&featured=true
如果我在选择第一个值后输入“b”...
那么我做错了什么,有人可以帮助我吗...
【问题讨论】:
标签: javascript jquery json autocomplete