views:

1341

answers:

4

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.

A: 

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.

John Sheehan
+1  A: 

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 :(

turezky
+1  A: 

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...

turezky
A: 

$("#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