我想在其选择事件上重新绑定自动完成插件,实际上我想从建议中删除所选项目。目前代码就像下面粘贴的一样,如果我在 select 函数中复制整个插件调用,那将是无稽之谈,因为我也必须将其粘贴到复制的调用的 select 事件中,依此类推。还有其他简单的方法吗?
$("#txtEmployeeID").autocomplete({
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
var matching = $.grep(newEmployeesWithIds, function (value) {
var name = value.value;
var id = value.id;
return matcher.test(name) || matcher.test(id);
});
response(matching);
},
select: function (event, value) {
$.ajax({
type: "GET", //GET or POST or PUT or DELETE verb
url: "http://localhost:46455/EmployeeSearchService.svc/GetEmployee", // Location of the service
data: {
"name": value.item.value
}, //Data sent to server
dataType: "Json", //Expected data format from server
processdata: "false", //True or False
success: function (employee) {//On Successfull service call
//INSERTING THE SELECTED OBJECT IN A UL
var employee = $.parseJSON(employee);
$('#ulSelectedEmployees').append($('<li>').attr('id', employee.TRGEmpID).css('padding', '5px').attr('class', 'ui-menu-item').html(employee.FullName).append($('<img>').attr('src', 'Styles/images/close_icon.png').attr('height', '20px').css('float', 'right').css('padding', '2px 13px')));
if ($('#ulSelectedEmployees').css('display') == 'none')
{
$('#ulSelectedEmployees').show();
$('#' + employee.TRGEmpID).fadeIn(700);
}
else
{
$('#' + employee.TRGEmpID).fadeIn(700);
}
/*DELETING THE ELEMENT TO REBIND AUTOCOMPLETE*/
for(var k = 0 ; k < newEmployeesWithIds.length ; k ++)
{
if(newEmployeesWithIds[k].id == employee.TRGEmpID)
break;
}
newEmployeesWithIds.splice(k, 1);
},
error: function (msg) {//On UnSuccessfull service call
ServiceFailed(msg);
}
});
}
});