1

我有一系列使用 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 `);`

如果这不是正确的方法,我不想让它输入东西。另外,无论如何我都无法让它工作。有什么建议么?

4

1 回答 1

1

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

从文档:

.autocomplete( "search" , [value] ) 触发搜索事件,当数据可用时,将显示建议;可以通过类似选择框的按钮在单击时打开建议。如果未指定 value 参数,则使用当前输入的值。可以使用空字符串和 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!!!
}
于 2011-02-01T06:52:47.023 回答