“搜索”按钮激活自动完成

发布于 2024-11-24 03:00:42 字数 96 浏览 3 评论 0原文

我正在使用 jQuery UI 自动完成插件。有没有办法可以使用“搜索”按钮来激活查询,而不是让自动完成文本框执行此操作?我的用户的互联网连接非常糟糕,他们很难使用自动完成功能。

I'm using jQuery UI Autocomplete plug-in. Is there a way I can use a ‘search’ button to activate the query instead of having the Autocomplete text box do it? My users have a real bad internet connection and the Autocomplete becomes hard for them to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2024-12-01 03:00:42

是的,可以做到。为了阻止搜索自然发生,搜索词的最小长度增加到(任意)1000 个字符。同时,搜索本身已在绑定到按钮 - 此信息记录在此页面上的事件选项卡中。 minLength 在触发搜索之前设置为 0(因此搜索实际上会触发),并在自动完成关闭时设置回 1000。

HTML:

<label for="tags">Tags: </label>
<input id="tags" />
<input type="button" value="Search"/>

JavaScript:

var availableTags = [
    'ActionScript',
    'AppleScript',
    'Asp',
    'BASIC',
    'C',
    'C++',
    'Clojure',
    'COBOL',
    'ColdFusion',
    'Erlang',
    'Fortran',
    'Groovy',
    'Haskell',
    'Java',
    'JavaScript',
    'Lisp',
    'Perl',
    'PHP',
    'Python',
    'Ruby',
    'Scala',
    'Scheme'
    ];

$('#tags').autocomplete({
    source: availableTags,
    minLength: 1000,
    close: function(event, ui) {
        $('#tags').autocomplete('option', 'minLength', 1000);
    }
});

$('input[type="button"]').click(function() {
    $('#tags').autocomplete('option', 'minLength', 0);
    $('#tags').autocomplete('search', $('#tags').val());
});

Yes, it can be done. To stop the search from occurring naturally, the minimum length for a search term is increased to (an arbitrary) 1000 characters. At the same time, the search itself has been programatically triggered in a .click() event bound to a button - this is documented in the Events tab on this page. The minLength is set to 0 (so the search will actually fire) just before triggering the search and it is set back to 1000 when the autocomplete closes.

HTML:

<label for="tags">Tags: </label>
<input id="tags" />
<input type="button" value="Search"/>

JavaScript:

var availableTags = [
    'ActionScript',
    'AppleScript',
    'Asp',
    'BASIC',
    'C',
    'C++',
    'Clojure',
    'COBOL',
    'ColdFusion',
    'Erlang',
    'Fortran',
    'Groovy',
    'Haskell',
    'Java',
    'JavaScript',
    'Lisp',
    'Perl',
    'PHP',
    'Python',
    'Ruby',
    'Scala',
    'Scheme'
    ];

$('#tags').autocomplete({
    source: availableTags,
    minLength: 1000,
    close: function(event, ui) {
        $('#tags').autocomplete('option', 'minLength', 1000);
    }
});

$('input[type="button"]').click(function() {
    $('#tags').autocomplete('option', 'minLength', 0);
    $('#tags').autocomplete('search', $('#tags').val());
});
‘画卷フ 2024-12-01 03:00:42

这个想法是关闭在文本添加事件上发生的自动完成。在焦点上,我们禁用自动完成功能,并在点击按钮或按回车键时启用它。

<script type="text/javascript">
$(window).load(function ()
{
    // Creates the autocomplete field
    $("#lookUpField").autocomplete(
    {
        source: ["Luis", "Erick", "Jorge", "Ana", "Anabel"],
        disabled: true
    });

    //disables the search trigger when field selected
    $("#lookUpField").focus(function ()
    {
       $("#lookUpField").autocomplete("disable");
    });


    // disables the field on keypress unless the 'enter' key
    // is pressed in which case it triggers a 'search'
    $('#lookUpField').keypress(function (e)
    {
       if (e.which == 13)
       {
          lookUpData();
       }
       else
       {
           $("#lookUpField").autocomplete("disable");
       }
   });
});

function lookUpData()
{
   $("#lookUpField").autocomplete("enable");
   $("#lookUpField").autocomplete("search");
}
</script>


<div>
    <input id="lookUpField" type="text" />
    <input type="button" value="Go" onclick="lookUpData()" />
</div>

The idea is to turn-off the autocomplete happening on text adding events. On focus we disable autocomplete and enable it upon hitting a button or pressing the enter key.

<script type="text/javascript">
$(window).load(function ()
{
    // Creates the autocomplete field
    $("#lookUpField").autocomplete(
    {
        source: ["Luis", "Erick", "Jorge", "Ana", "Anabel"],
        disabled: true
    });

    //disables the search trigger when field selected
    $("#lookUpField").focus(function ()
    {
       $("#lookUpField").autocomplete("disable");
    });


    // disables the field on keypress unless the 'enter' key
    // is pressed in which case it triggers a 'search'
    $('#lookUpField').keypress(function (e)
    {
       if (e.which == 13)
       {
          lookUpData();
       }
       else
       {
           $("#lookUpField").autocomplete("disable");
       }
   });
});

function lookUpData()
{
   $("#lookUpField").autocomplete("enable");
   $("#lookUpField").autocomplete("search");
}
</script>


<div>
    <input id="lookUpField" type="text" />
    <input type="button" value="Go" onclick="lookUpData()" />
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文