Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Autocomplete: Event-select

I am trying to submit a form when an item is selected from the menu. I set class on the search form and I am using the event select for it which is found here: http://docs.jquery.com/UI/Autocomplete#event-select

Now, when you select an item using the keyboard (UP and Down), it works. But if you select an item using the mouse, it gives you the value which is previously typed.

Check this screenr: http://www.screenr.com/7T0s

This is what I use for submitting:

$("#searchform-input").autocomplete({     select: function (a, b) {         $(".searchform1").submit()     } }); 
like image 482
jQuerybeast Avatar asked Oct 11 '11 22:10

jQuerybeast


People also ask

How to select autocomplete value in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How to call autocomplete in jQuery?

You can use the following script to toggle the autocomplete manually: var textbox = $('input#mainSearchBox'); var autocompleteBox = textbox. autocomplete('widget'); // toggle the autocomplete widget autocompleteBox.is(':hidden') ? textbox.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })

What is UI autocomplete?

Autocomplete mechanism is frequently used in modern websites to provide the users a list of suggestion while typing the beginning word in the text box. It facilitates the user to select an item from the list, which will be displayed in the input field.


1 Answers

This is because the select event's default behavior is executed after your event handler is finished running (so that you can cancel it if you so choose).

This means that when you click something, your form is submitted before the widget has a chance to populate the input properly.

You should be able to fix this by doing what the widget normally does for you:

$("#searchform-input").autocomplete({     select: function (a, b) {         $(this).val(b.item.value);         $(".searchform1").submit()     } }); 

Now, what you may be wondering is yes, but why does it work when I use the keyboard?

This happens because the focus event actually populates the focused item in the input (look closely; you'll see the input populated as you move up and down the list). When you mouseover an item, the focus event is called, populating the input. When you select something using the enter key, the correct value happens to be in the input because of the focus event.

like image 108
Andrew Whitaker Avatar answered Oct 11 '22 11:10

Andrew Whitaker