简体   繁体   English

jQuery-UI - 多个自动填充字段共享结果

[英]jQuery-UI - Multiple autocomplete fields shared results

I am trying to build a form with multiple autocompleted fields. 我正在尝试构建一个包含多个自动填充字段的表单。 I am using jQuery-ui 1.8.21 to do the autocomplete via the .autocomplete binding. 我正在使用jQuery-ui 1.8.21通过.autocomplete绑定进行自动完成。 I have 5 different fields which have autocomplete bound to them, and each send their suggestions to a different div outside of the form, at the end of the page. 我有5个不同的字段,它们都绑定了自动完成,并且每个字段都在页面末尾将其建议发送到表单之外的不同div。

the form looks like this: 表单看起来像这样:

<form>
    <input type="text" name="afield" />
    <input type="text" name="bfield" />
</form>
<div id="a_complete">
</div>
<div id="b_complete">
</div>

the jQuery code like this: 像这样的jQuery代码:

$(function(){
    $("[name=afield]").autocomplete({
        source: "/data/source/a",
        open: function(event, ui) {
        $('ul.ui-autocomplete')
            .removeAttr('style').hide().appendTo('#a_complete').show();
        }
    });
    $("[name=bfield]").autocomplete({
        source: "/data/source/b",
        open: function(event, ui) {
        $('ul.ui-autocomplete')
            .removeAttr('style').hide().appendTo('#b_complete').show();
        }
    });
});

The problem with this is that when I begin typing in bfield , the matched results for afield are also displayed in *b_complete* as well as the results for bfield . 这样做的问题是,当我在bfield开始打字,对于更远的匹配结果也显示在* b_complete *以及结果的bfield。

I have tried setting cacheLength to 0 or 1, and use flushCache() on different events ( search, open, close, select ) to no success. 我尝试将cacheLength设置为0或1,并对不同的事件( 搜索,打开,关闭,选择 )使用flushCache( ),但没有成功。

This is only a cosmetic issue though as when I click on a result it will update the proper field, and when I walk through the results with the arrow keys they only return the results for the correct field. 这只是一个美化问题,因为当我点击结果时它会更新正确的字段,当我用箭头键遍历结果时,它们只返回正确字段的结果。

你需要使你的$('ul.ui-autocomplete')唯一的,例如,使用另一个类定义或“id”,这样它就是$('ul.ui-autocomplete#a')$('ul.ui-autocomplete#b')

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

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