jQuery 自动完成 - 当用户单击带有可变文本的文本输入框时如何开始搜索
我有一系列文本输入框,它们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这是 jQueryUI 1.8 的自动完成功能,而不是插件,您需要调用搜索方法:
来自文档:
所以...
Assuming this is the autocomplete from jQueryUI 1.8 and not a plugin, You need to call the search method:
From the docs:
so...