I have a autocomplete ui in which there is a pre defined User list as source.
When a user selects a user from the list, it’s id is saved in a hidden field. Up to here it is fine. Now, If the user writes a user name that is not in the autocomplete list then I need to change the value of hidden field to 0 and do other tasks. But i am not able to do this…
I tried using the .change event, but its not working.
Here is the code.
$.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;
}
}
});
So, How Basically I need is to, Set UserId when user selects the value from autocomplete, or set userid=0 when user inputs new value.
UPDATE
Forgot to add, the .change event doesn’t work because, every time the value of ui.item==null is true and the alert gave exception due to it.
Guess what, Little bit of tweaking, and I did it myself with some help from friend.
here is the code. It is on the
.changeevent.I did was searched through my
dataone by one, and when not found cleared the User id.