How do you trigger autocomplete “select” event manually in jQueryUI?

后端 未结 10 1372
别跟我提以往
别跟我提以往 2020-12-01 09:28

I\'m using jQueryUI autocomplete, and I have a function mapped to the select event, e.g.:

$(\"#someId\").autocomplete({
    source: someData,
    select: fun         


        
相关标签:
10条回答
  • 2020-12-01 09:37

    If we want to trigger search of something particularly:

    $('#search-term').autocomplete('search', 'searching term');
    

    or just

    $('#search-term').autocomplete('search');
    
    $('#search-term').autocomplete({
        ...
        minLength: 0, // with this setting
        ...
    });
    
    0 讨论(0)
  • 2020-12-01 09:37

    I found very simple way to make it work. Answers above did not work for me.

    My input definition:

    <div class="search_box">
    <input type="text" class="inp disabled" id="search-user-id" placeholder="Search by user name or email" />
    </div>
    

    The autocomplete definition:

    $('#search-user-id').autocomplete({
        minChars: 3,
        paramName: 'searchTerm',
        deferRequestBy: 200, // This is to avoid js error on fast typing
        serviceUrl: '${pageContext.request.contextPath}/settings/reset/psw/query',
        type: 'POST',
        onSelect : function(suggestion) {
            showUserData(suggestion.dto);
        }, 
        onSearchError: function (query, jqXHR, textStatus, errorThrown) {
            $('.cev-error-fields.reset-password').find('.error_msg').text(errorThrown).show().fadeOut(7000);
        }
    }); 
    

    Trigger it: I am triggering from an ajax defined in other page, I do not put the ajax here for simplicity. Inside the ajax response I just trigger it this way:

            $('#search-user-id').val(username);
            $('#search-user-id').focus();
    

    It worked.

    0 讨论(0)
  • 2020-12-01 09:39

    From outside:

    $($('#someId').data('autocomplete').menu.active).find('a').trigger('click');
    

    An example how to setup select triggering at pressing of horizontal arrows keys from inside of "open" autocomplete event:

    $('#someId').autocomplete({
        open: function(event, ui) {
            $(this).keydown(function(e){
                /* horizontal keys */
                if (e.keyCode == 37 || e.keyCode == 39) {
                   $($(this).data('autocomplete').menu.active).find('a').trigger('click');
                }
            });
        }
    });
    

    Unfortunately that nice way how to solve this marked as "success" did not work at my app in chromium 140.0.835.200

    0 讨论(0)
  • 2020-12-01 09:43

    1 line solution

    $('.ui-menu-item .ui-corner-all:contains(\"123")').autocomplete({autoFocus:true}).mouseenter().click()
    
    0 讨论(0)
  • 2020-12-01 09:51

    Here's what worked for me:

    $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', {item:{value:$(this).val()}});
    
    0 讨论(0)
  • 2020-12-01 09:51
    $('#someId').data('uiAutocomplete')._trigger('select');
    
    0 讨论(0)
提交回复
热议问题