jQuery 自动完成 - 当用户单击带有可变文本的文本输入框时如何开始搜索

发布于 2024-10-15 05:29:17 字数 1059 浏览 4 评论 0原文

我有一系列文本输入框,它们使用 jQuery 自动完成来帮助用户选择适当的项目。该项目必须匹配,否则该字段必须留空。这部分工作正常。

现在,这需要用户单击该字段,然后在进行选择之前开始键入匹配的文本。通常,但并非总是,脚本已知前几个匹配字符(但它们随着字段的不同而变化)。如果字符已知,我想帮助用户,允许他们单击输入字段,然后进行搜索并使用这几个字符开始搜索。这些字符可能是错误的,如果是这样,用户可以删除它们并输入自己的输入来选择匹配项。

我想我想要的只是在用户单击输入时为用户输入它们。然后这神奇地触发了自动搜索,他们从那里获取它。但最好的方法是什么?

我有:

$("#input_thing").autocomplete('query.php', {
    width: 300,
    multiple: false,
    matchContains: false,
    formatItem: formatItem,
    formatResult: formatResult,
    mustMatch: true,
    cacheLength: 1,
    extraParams: {
        "category": function () {
            return $("#category option:selected").val()
        },
        "order": "1"
    }
}); // edit note:  added missing `});`

$("#input_thing").focus(function () {
    if ($("#search_starting_text").text().length > 0 && $("#input_thing").text() == 0) {
        //This meets my conditions for helping the user but not sure what to do here!!!
    }
}); // edit note:  added missing `);`

如果这不是正确的方法,我不希望它输入内容。而且,我无论如何也无法让它工作。有什么建议吗?

I have a series of text input boxes that use jQuery autocomplete to help the user select an appropriate item. The item must match, otherwise the field must be left blank. This part works fine.

Now, this requires the user clicking on the field, then starting to type matching text before doing the selction. Often, but not always, the first few matching characters are known by the script (but they change from field to field). If the characters are known, I would like to help the user by allowing them to click on the input field, then having the search come up and begin the search using those few characters. The characters may be wrong, if so the user can just delete them and enter their own input to select a match.

What I think I want is to just basically have them typed in for the user when they click the input. Then that magically triggers the autosearch and they take it from there. But what's the best way to do this?

I have:

$("#input_thing").autocomplete('query.php', {
    width: 300,
    multiple: false,
    matchContains: false,
    formatItem: formatItem,
    formatResult: formatResult,
    mustMatch: true,
    cacheLength: 1,
    extraParams: {
        "category": function () {
            return $("#category option:selected").val()
        },
        "order": "1"
    }
}); // edit note:  added missing `});`

$("#input_thing").focus(function () {
    if ($("#search_starting_text").text().length > 0 && $("#input_thing").text() == 0) {
        //This meets my conditions for helping the user but not sure what to do here!!!
    }
}); // edit note:  added missing `);`

I'm not looking to have it type stuff in if that's not the right way to do this. Also, I couldn't get it to work anyway. Any suggestions?

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

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

发布评论

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

评论(1

本宫微胖 2024-10-22 05:29:17

假设这是 jQueryUI 1.8 的自动完成功能,而不是插件,您需要调用搜索方法:

来自文档:

.autocomplete( "搜索" , [值] )
触发搜索事件,当
数据可用,然后将显示
建议;可以由
类似选择框的按钮来打开
单击时的建议。如果没有值
指定参数,当前
使用输入的值。可以调用
带有空字符串和 minLength: 0
显示所有项目。

所以...

if ($("#search_starting_text").text().length > 0 && $("#input_thing").text() == 0) {

    $("#input_thing").autocomplete( "search",$("#search_starting_text").text());
        //This meets my conditions for helping the user but not sure what to do here!!!
}

Assuming this is the autocomplete from jQueryUI 1.8 and not a plugin, You need to call the search method:

From the docs:

.autocomplete( "search" , [value] )
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.

so...

if ($("#search_starting_text").text().length > 0 && $("#input_thing").text() == 0) {

    $("#input_thing").autocomplete( "search",$("#search_starting_text").text());
        //This meets my conditions for helping the user but not sure what to do here!!!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文