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

时间:2011-02-01 04:56:04

标签: jquery jquery-autocomplete jquery-ui-autocomplete

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

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

1 个答案:

答案 0 :(得分:1)

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

来自文档:

  

.autocomplete(“搜索”,[值])   触发搜索事件,何时触发   数据可用,然后将显示   建议;可以用来使用   像selectbox一样的按钮来打开   点击时的建议。如果没有价值   参数是指定的,当前的   使用输入的值。可以叫   用空字符串和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!!!
}