doovers
doovers

Reputation: 8675

JqueryUI Autocomplete - Handling no selection

There are lots of similar questions to this on SO but none of them have really provided an answer to my particular case. Or at least I can't figure it out!

I have a form with an autocomplete field which is used to populate other fields if a record exists.

I have found that some users type in a value and click to the next cell using the mouse and therefore do not make a selection from the autocomplete list. It's possible that they have typed a value with a matching record in the input field and in that case I want the form to be populated with the appropriate data. Or if it doesn't, then clear the form.

The only way I can think of to check the value exists in the database is to use the change event to make an ajax call to retrieve the data but that doesn't seem like a very elegant solution and I'd be very surprised if there isn't a better way to do this since it seems to me that this would be a very common scenario...

Is there a way to retain the autocomplete array and check it against the input value in the change event? Or how else can I do it?

Upvotes: 1

Views: 48

Answers (1)

John - Not A Number
John - Not A Number

Reputation: 659

What you can do is stash away a copy of the data returned in the ajax call's success callback.

You can then add a blur event handler to the autocomplete input, so it'll be called whenever the user clicks away to the next field. In the event handler, check the stashed ajax data, and if there was only a single possible match, use that to populate the input.

Upvotes: 1

Related Questions