0
我使用jQuery UI
自動完成來爲消息收件人建議用戶名。但由於郵件可以發送給很多收件人,因此我不想在選擇元素後自動完成清除輸入。我通過select:
這個動作嘗試了這個,但它不起作用,就像我想讓它工作一樣。收件人字段應爲下面提到的字符串添加多個收件人後:jQuery UI - 將選定的項目附加到輸入
Username1, Username2, Username3
這是我的代碼:
$("#message_recipient").autocomplete({
source: function(request, response) {
if($('#message_recipient').val().indexOf(",") != -1){
var pieces = $.trim($('#message_recipient').val()).split(/[\s,]+/);
var last_piece = $.trim(pieces[pieces.length-1]);
} else {
var last_piece = $.trim($('#message_recipient').val());
}
if(last_piece!=""){
$.ajax({
url: baseurl+"users/search_for_user",
dataType: "json",
type: "POST",
data: {
user_name: last_piece
},
success: function(data) {
response($.map(data.result, function(item) {
return {
label: item.user_name,
value: item.user_name
}
}));
}
});
}
},
minLength: 2,
select: function(event, ui) {
$('#message_recipient').val($('#message_recipient').val()+ui.item.label+", ");
},
open: function() {
},
close: function() {
}
});