set value to jquery autocomplete combobox

后端 未结 5 1544
星月不相逢
星月不相逢 2021-01-02 07:58

I am using jquery autocomplete combobox and everything is ok. But I also want to set specific value through JavaScript like $(\"#value\").val(\"somevalue\") and

5条回答
  •  一生所求
    2021-01-02 08:12

    I really like what andyb did, but I needed it to do a little more around event handling to be able to handle triggering the a change event because "selected" doesn't handle when hitting enter or losing focus on the input (hitting tab or mouse click).

    As such, using what andyb did as a base as well as the latest version of the jQuery Autocomplete script, I created the following solution: DEMO

    • Enter: Chooses the first item if menu is visible
    • Focus Lost: Partial match triggers not found message and clears entry (jQuery UI), but fully typed answer "selects" that value (not case sensative)

    How Change method can be utlized:

    $("#combobox").combobox({
        selected: function (event, ui) {
            $("#output").text("Selected Event >>> " + $("#combobox").val());
        }
    })
    .change(function (e) {
        $("#output").text("Change Event >>> " + $("#combobox").val());
    });
    

    Hopefully this helps others who need additional change event functionality to compensate for gaps that "selected" leaves open.

提交回复
热议问题