jQuery UI autocomplete submit onclick result

后端 未结 2 1229
别那么骄傲
别那么骄傲 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:34

    From: http://api.jqueryui.com/autocomplete/#event-select

    select - Type:autocompleteselect

    Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item. Canceling this event prevents the value from being updated, but does not prevent the menu from closing.

    Code examples

    Supply a callback function to handle the select event as an init option.

    $( ".selector" ).autocomplete({
       select: function(event, ui) { ... }
    });
    

    Bind to the select event by type: autocompleteselect.

    $( ".selector" ).bind( "autocompleteselect", function(event, ui) {
      ...
    });
    

    So you would use:

    EDIT: (modified in case user does not select anything)

    $("#vsearch").autocomplete({
        source: "ajax/search.php",
        minLength: 2,
        select: function(event, ui) {
            if(ui.item){
                $('#vsearch').val(ui.item.value);
            }
            $('#search').submit();
        }
    });
    

    If I am sure of what you are wanting to do.

提交回复
热议问题