jQuery AutoComplete Trigger Change Event

后端 未结 13 1015
陌清茗
陌清茗 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:26

    It's better to use the select event instead. The change event is bound to keydown as Wil said. So if you want to listen to change on selection use select like that.

    $("#yourcomponent").autocomplete({  
        select: function(event, ui) {
            console.log(ui);
        }
    });
    
    0 讨论(0)
  • 2020-11-27 16:28

    Here is a relatively clean solution for others looking up this topic:

    // run when eventlistener is triggered
    $("#CompanyList").on( "autocompletechange", function(event,ui) {
       // post value to console for validation
       console.log($(this).val());
    });
    

    Per api.jqueryui.com/autocomplete/, this binds a function to the eventlistener. It is triggered both when the user selects a value from the autocomplete list and when they manually type in a value. The trigger fires when the field loses focus.

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

    The programmatically trigger to call the autocomplete.change event is via a namespaced trigger on the source select element.

    $("#CompanyList").trigger("blur.autocomplete");
    

    Within version 1.8 of jquery UI..

    .bind( "blur.autocomplete", function( event ) {
        if ( self.options.disabled ) {
            return;
        }
    
        clearTimeout( self.searching );
        // clicks on the menu (or a button to trigger a search) will cause a blur event
        self.closing = setTimeout(function() {
            self.close( event );
            self._change( event );
        }, 150 );
    });
    
    0 讨论(0)
  • 2020-11-27 16:29

    Another solution than the previous ones:

    //With trigger
    $("#CompanyList").trigger("keydown");
    
    //With the autocomplete API
    $("#CompanyList").autocomplete("search");
    

    jQuery UI Autocomplete API

    https://jsfiddle.net/mwneepop/

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

    Here you go. It's a little messy but it works.

    $(function () {  
      var companyList = $("#CompanyList").autocomplete({ 
          change: function() {
              alert('changed');
          }
       });
       companyList.autocomplete('option','change').call(companyList);
    });
    
    0 讨论(0)
  • 2020-11-27 16:35

    I was trying to do the same, but without keeping a variable of autocomplete. I walk throught this calling change handler programatically on the select event, you only need to worry about the actual value of input.

    $("#CompanyList").autocomplete({ 
        source: context.companies, 
        change: handleCompanyChanged,
        select: function(event,ui){
            $("#CompanyList").trigger('blur');
            $("#CompanyList").val(ui.item.value);
            handleCompanyChanged();
        }
    });
    
    0 讨论(0)
提交回复
热议问题