我有一个自动完成的用户界面,其中有一个预先定义的用户列表作为源。 当用户从列表中选择一个用户时,它的ID被保存在一个隐藏字段中。到此为止,这很好。现在,如果用户写入的用户名不在自动完成列表中,那么我需要将隐藏字段的值更改为0并执行其他任务。但我无法做到这一点...在Jquery自动完成Ui中搜索文本
我试过使用.change事件,但它不工作。 这是代码。
$.ajax({
type: "POST",
url: "CHService.asmx/GetClient",
dataType: "json",
data: "{}",
contentType: "application/json; charset=utf-8",
success: function (data) {
$('#txtName').autocomplete({
minLength: 0,
source: function (req, responseFn) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp("^" + re, "i");
var a = $.grep(data.d, function (item, index) {
return matcher.test(item.value);
});
responseFn(a);
}, //Searches user input with first letter of ...//data.d
select: function (event, ui) {
$('#txtName').val(ui.item.value);
$('#HFuser').val(ui.item.Name);
alert('Select' + ui.item.Name);
//return false;
},
change: function (event, ui) {
alert('Change' + ui.item.Name);
if (ui.item == null) {
$('#HFuser').val(0);
//return false;
}
}
});
所以,我需要怎样基本上是,设置UserId
当用户从自动完成选择值,或者设置userid=0
当用户输入新的价值。
UPDATE
忘了补充,该.change事件不会工作,因为,每一次的ui.item==null is true
值和警报了例外,由于它。
直到现在http://stackoverflow.com/questions/3299839/jquery-ui-autocomplete-with-hybrid-text-id-search是我接近的唯一的东西(见第一个答案)。但那是另一种方式。 – Ruchan
是那里的一个有效的财产? ui.item.Name? autosuggest默认只包含ui.item.label和ui.item.value。你没有在控制台中发现错误? – chrisvillanueva
没有没有错误的。 .Name是在我的json列表中定义的,所以我使用它。 无论如何,我想出了我的问题。查看下面的答案,如果有任何建议或错误 – Ruchan