jQuery Autocomplete: Event-select

后端 未结 2 1211
花落未央
花落未央 2021-02-02 10:11

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

相关标签:
2条回答
  • 2021-02-02 10:25

    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.

    0 讨论(0)
  • 2021-02-02 10:33

    Hehe. Quite tricky this one, but incredibly simple to solve. Just delay the function 500 milliseconds, after the select event. It works perfectly. JOB DONE!! :)

    $("#searchform-input").autocomplete({
    select: function (a, b) {
        setTimeout(submit,500);
    }});
    
    0 讨论(0)
提交回复
热议问题