jQuery UI autocomplete submit onclick result

后端 未结 2 1234
别那么骄傲
别那么骄傲 2020-12-29 09:38

I\'ve been trying to figure out how to submit the form when the person selects the result from choices of the autocomplete. It needs to work with a mouse click or enter butt

2条回答
  •  醉话见心
    2020-12-29 10:35

    Actually, you don't need to target the for element and form by ID... since that information is already passed to the select function via the Event object. It's better to get the form this way because you may have multiple forms on a page that you want to apply autocomplete to. Or you may want autocomplete on multiple elements.

    Also, the syntax in the other answer is wrong. JQuery uses .val() not .value() to set an input's value.

    Here's a corrected example:

    $("#vsearch").autocomplete({
        source: "ajax/search.php",
        minLength: 2,
        select: function(event, ui) {
            //assign value back to the form element
            if(ui.item){
                $(event.target).val(ui.item.value);
            }
            //submit the form
            $(event.target.form).submit();
        }
    });
    

提交回复
热议问题