开发者

Jquery UI autocomplete; minLength:0 issue

开发者 https://www.devze.com 2023-02-02 20:15 出处:网络
I am using a JQuery UI auto-complete. I am specifying the min length. If I specify minLength:2 I need to type 2 characters before the serv开发者_StackOverflow中文版ice fires. If I specify minLength:1

I am using a JQuery UI auto-complete. I am specifying the min length. If I specify minLength:2 I need to type 2 characters before the serv开发者_StackOverflow中文版ice fires. If I specify minLength:1 then I only need to type one character before the source service fires.

However, when I specify minLength:0 I still need to type one character. So whats the point of 0? how is it any different than 1? The expected, and desired, behavior would be for it to fire as soon as the input has focus...?

Ideas?

Update: For the most part karim's solution below works, however when clicking on the input the options come up and when one is clicked the source is resubmitted the empty string rather than the new option that was just selected, so the auto-complete options that come up after selecting an option are than same as if the box had been empty.


Check out the search method documentation:

Triggers a search event, which, when data is available, then will display the suggestions; can be used by a selectbox-like button to open the suggestions when clicked. If no value argument is specified, the current input's value is used. Can be called with an empty string and minLength: 0 to display all items.

var source = ['One', 'Two', 'Three', 'Four'];

var firstVal = source[0];

$("input#autocomplete").autocomplete({
    minLength: 0,
    source: source,
}).focus(function() {
    $(this).autocomplete("search", $(this).val());
});
.ui-menu-item{
  background : rgb(215, 215, 215);;  
  border : 1px solid white;
  list-style-type: none;
  cursor : pointer;
}

.ui-menu-item:hover{
  background : rgb(200, 200, 200);
}


.ui-autocomplete{
   padding-left:0;
   margin-top  : 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
<input id="autocomplete"/>&nbsp;<input id="submit" type="submit"/>


I understand through experiment why the accepted answer (I up-voted) can be considered slightly incomplete. I've added a little bit of intent to make the UX more combo-box-like:

$('#Carrier').autocomplete(
{
    source: '@Url.Action("AutocompleteCarrier", "Client")',
    minLength: 0,
    select: function (event, data) {
        $(this).autocomplete('disable');
    }
})
.blur(function(){
    $(this).autocomplete('enable');
})
.focus(function () {
    $(this).autocomplete('search', '');
});


I had the same problem, and solved it this way. I think the code below makes it much clearer what is the text that is sent to the autocompletion service when the field gets focus. In particular, it is clear why it works when the field already contains something.

$(function() {
$("input#autocomplete").autocomplete({
        source: "completion.html",
        minLength: 0,
        select: function( event, ui ) {}
    });
});
$("input#autocomplete").focus(function() {
    $(this).autocomplete("search", $(this).val());
});


I had the same problem and I got how to workaround this issue.

The problem is that when an option has been selected the input will be focused, this will run search without any filter and will show the autocomplete again.

This problem doesn't happen when you select by keyboard because the input is already in focus, so the code which is inside focus will not run again.

So to workaround this issue we have to know when the focus is triggered by any mouse/keyboar event.

jQuery("input#autocomplete").focus(function(e) {
    if(!e.isTrigger) {
        jQuery(this).autocomplete("search", "");
    }
    e.stopPropagation();
});

e.isTrigger is used by jQuery to identify when event has been triggered automatically or by user.

working demo


This actually works! Tested on IE, FireFox

$("input#autocomplete").autocomplete({
minLength: 0,
source: source,
}).focus(function() {
        setTimeout( "if ($('#input#autocomplete').val().length == 0) $('#input#autocomplete').autocomplete(\"search\", \"\"); ",1);
    });


the suggestion menu shows up for the second time because clicking on an item in suggestion menu causes text box to regain focus, however the text value is not updated when the focus is fired again.After the textbox regains focus, the suggestion menu is fired.

I dont know how we could fix this, but let me know if anyone of you are facing the similar problem


I know that this is a very old thread, but I am having the exact same problem, and none of the solutions provided above seem to work... I assume that this is because of updates in Jquery? If someone could post a working updated (i.e. jquery-1.12.4.js // 1.12.1/jquery-ui.js) example, that would be fantastic.

0

精彩评论

暂无评论...
验证码 换一张
取 消