jQuery AutoComplete Trigger Change Event

后端 未结 13 1016
陌清茗
陌清茗 2020-11-27 15:50

How do you trigger jQuery UI\'s AutoComplete change event handler programmatically?

Hookup

$(\"#CompanyList\").autocomplete({ 
    s         


        
相关标签:
13条回答
  • 2020-11-27 16:35

    Well it works for me just binding a keypress event to the search input, like this:

    ... Instantiate your autofill here...
    
        $("#CompanyList").bind("keypress", function(){
        if (nowDoing==1) {
            nowDoing = 0;
            $('#form_459174').clearForm();
        }        
    });
    
    0 讨论(0)
  • 2020-11-27 16:36

    this will work,too

    $("#CompanyList").autocomplete({
      source : yourSource,
      change : yourChangeHandler
    })
    
    // deprecated
    //$("#CompanyList").data("autocomplete")._trigger("change")
    // use this now
    $("#CompanyList").data("ui-autocomplete")._trigger("change")
    
    0 讨论(0)
  • 2020-11-27 16:39

    The simplest, most robust way is to use the internal ._trigger() to fire the autocomplete change event.

    $("#CompanyList").autocomplete({
      source : yourSource,
      change : yourChangeHandler
    })
    
    $("#CompanyList").data("ui-autocomplete")._trigger("change");
    

    Note, jQuery UI 1.9 changed from .data("autocomplete") to .data("ui-autocomplete"). You may also see some people using .data("uiAutocomplete") which indeed works in 1.9 and 1.10, but "ui-autocomplete" is the official preferred form. See http://jqueryui.com/upgrade-guide/1.9/#changed-naming-convention-for-data-keys for jQuery UI namespaecing on data keys.

    0 讨论(0)
  • 2020-11-27 16:46

    They are binding to keydown in the autocomplete source, so triggering the keydown will case it to update.

    $("#CompanyList").trigger('keydown');
    

    They aren't binding to the 'change' event because that only triggers at the DOM level when the form field loses focus. The autocomplete needs to respond faster than 'lost focus' so it has to bind to a key event.

    Doing this:

    companyList.autocomplete('option','change').call(companyList);
    

    Will cause a bug if the user retypes the exact option that was there before.

    0 讨论(0)
  • 2020-11-27 16:46

    This post is pretty old, but for thoses who got here in 2016. None of the example here worked for me. Using keyup instead of autocompletechange did the job. Using jquery-ui 10.4

    $("#CompanyList").on("keyup", function (event, ui) {
        console.log($(this).val());
    });
    

    Hope this help!

    0 讨论(0)
  • 2020-11-27 16:48

    You have to manually bind the event, rather than supply it as a property of the initialization object, to make it available to trigger.

    $("#CompanyList").autocomplete({ 
        source: context.companies
    }).bind( 'autocompletechange', handleCompanyChanged );
    

    then

    $("#CompanyList").trigger("autocompletechange");
    

    It's a bit of a workaround, but I'm in favor of workarounds that improve the semantic uniformity of the library!

    0 讨论(0)
提交回复
热议问题