I've got jQuery Autocomplete (UI 1.6rc2) up and running fine and when the user picks an item, it updates a hidden form value with the associated ID. How do I set the hidden form value to '0' when the text entered does not match a result from the autocomplete list? In this case, I'll be creating a new entry.
6 Answers
I did this in the autocomplete function:
change: function(event, ui){
$(this).next("input[id^=person_id]").val('');
return false;
After the user selects the option and it populates my hidden input with the item ID, if any changes occur to the visible input, the hidden input value is cleared. Works like a charm!

- 46
- 2
You can also use the "search" option with the autocomplete.
search: function(event, ui){
$(this).next("input[id^=person_id]").val('');
}
Seems to work. It does trigger after the delay and minlength, but does do a nice clear if you've set it to minlength=0.

- 21
- 1
I'm stuck with the same problem... Looks like there's no event risen when the values don't match... However, it seems that some workaround has been found...
By Xavier from the replies on this thread:
selected: function() {
if(listItems.filter(”.” + CLASSES.ACTIVE)[0]){
return data && data[ listItems.filter("." + CLASSES.ACTIVE)[0].index ];
} else {
if (options.notFound){
options.notFound();
}
}
}
That's not the best solution I reckon... Any other ideas? Yeah, and I didn't resolve how can I get use of this particular solution either :(

- 856
- 4
- 11
- 16
Ooh, I think I found a trick how do surpass all these events...
Simply, add this to the extraParams
along withi othor params sent to the autocomplete
extraParams: {
x: function(){ $("#targetField").val(''); }
}
This one simply empties the field while sending the request, and if no result is found or nothing is selected and some new custom value is entered it just remains empty...

- 856
- 4
- 11
- 16
According to the comment thread here it doesn't appear possible. I guess I'll just check the entered value against the db and if it doesn't exist, create a new one.

- 77,456
- 30
- 160
- 194
$("#txtSearchProvider").autocomplete("../WebServices/PageMethods/AutoComplete.aspx",
{
minChars: 3,
formatItem: function(data, i, n, value) {
return value.split("-z-")[0];
},
formatResult: function(data, value)
{
return value.split("-z-")[0];
}
}
);
$("#txtSearchProvider").result(function(event, data, formatted) {
$("#txtSearchProviderHidden").val(data[0].split("-z-")[1]);
});
Response from : ../WebServices/PageMethods/AutoComplete.aspx
Data 1, an Corp-z-29310 Data 2, Almazan & Barbara, LLP-z-35745 Dos Santos Santos-z-36327 Calzano & Salzano-z-39295

- 8,414
- 11
- 70
- 116