简体   繁体   English

Jquery UI自动完成; minLength:0期

[英]Jquery UI autocomplete; minLength:0 issue

I am using a JQuery UI auto-complete. 我正在使用JQuery UI自动完成。 I am specifying the min length. 我指定最小长度。 If I specify minLength:2 I need to type 2 characters before the service fires. 如果我指定minLength:2我需要在服务触发前键入2个字符。 If I specify minLength:1 then I only need to type one character before the source service fires. 如果我指定minLength:1那么我只需要在源服务触发之前键入一个字符。

However, when I specify minLength:0 I still need to type one character. 但是,当我指定minLength:0我仍然需要输入一个字符。 So whats the point of 0 ? 那么0点是什么意思? how is it any different than 1 ? 怎么跟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 . 更新:在大多数情况下,karim的解决方案可以正常工作,但是当点击输入时会出现选项,当单击一个时,源会重新提交空字符串而不是刚刚选中的新选项 ,因此自动完成选项选择一个选项后,如果该框已空,则相同

Check out the search method documentation: 查看search方法文档:

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. 如果未指定value参数,则使用当前输入的值。 Can be called with an empty string and minLength: 0 to display all items. 可以使用空字符串和minLength:0来调用以显示所有项目。

 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: 我添加了一些意图,使UX更像组合框:

$('#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. 问题是,当选择了一个选项时,输入将被聚焦,这将在没有任何过滤器的情况下运行search并再次显示autocomplete

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. 当您通过键盘选择,因为此问题不会发生input已经在重点,所以这里面的代码focus将不再运行。

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. jQuery使用e.isTrigger来识别event何时被自动触发或由用户触发。

working demo 工作演示

This actually works! 这实际上有效! Tested on IE, FireFox 在IE,FireFox上测试过

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

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? 我知道这是一个非常老的线程,但我遇到了完全相同的问题,上面提供的解决方案似乎都没有工作......我认为这是因为Jquery的更新? If someone could post a working updated (ie jquery-1.12.4.js // 1.12.1/jquery-ui.js) example, that would be fantastic. 如果有人可以发布一个工作更新(即jquery-1.12.4.js // 1.12.1 / jquery-ui.js)的例子,那就太棒了。

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 我不知道我们如何解决这个问题,但如果你们中的任何人面临类似的问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM