如何使 jQuery 自动完成仅替换某些单词 - 而不是完整字符串?
我正在使用 bassistance.de jQuery 自动完成 功能。
文字如“你好@john” 我设置了“自动完成”功能,仅当字符@出现在单词中时才运行。 但是当我单击所需的项目时,它会替换我的全文。 我该怎么做 - 仅替换“@john”? 或者也许还有另一个 jQuery 自动完成插件,它有这样的能力?
$('#input_line').autocomplete('data.php', {
extraParams: {input: function() {
return GetTextareaWord("input_line");
}
}
});
I am using bassistance.de jQuery Autocomplete function.
Have text like "hello to you @john"
I made, Autocomplete run only when character @ is in word.
But when I click on the needed item, it replaces me full text.
How can I do so - to replace only "@john"?
Or maybe there is another Autocomplete plugin for jQuery, which has such ability?
$('#input_line').autocomplete('data.php', {
extraParams: {input: function() {
return GetTextareaWord("input_line");
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您为自动完成插件发布的链接所述
我建议您查看 jQueryUI 自动完成。使用该插件,您可以将自定义事件处理程序绑定到 select 事件(当选择列表中的一项):
取消默认事件(即替换字段的整个值),并使用您自己的逻辑适当地更新字段(在本例中,可能只是将值插入到文本中)用户光标在该时间点所在的字段)。
似乎可行,特别是如果您已经弄清楚如何仅在需要时显示自动完成菜单。
As noted on the link you posted for the autocomplete plugin
I would recommend checking out jQueryUI autocomplete. With that plugin, you can bind a custom event handler to the select event (fired when an item in the list is selected):
Cancel the default event (which is replacing the entire value of the field), and use your own logic to update the field appropriately (in this case, it may be just inserting the value into the text field where the user's cursor is at that point in time).
Seems doable, especially if you've figured out how to only show the autocomplete menu when you want it.
我一直在研究与自动完成标签非常相似的东西。我有一些基本的代码可以工作,但我有点羞于在这里发布,因为它是边缘垃圾。它需要大量的清理和重构,我将在接下来的几天内完成。尽管如此,它以类似于@Andrew建议的方式满足您的需要。
我将在接下来的几天内实现此功能并将其打包为 jQuery 插件。我将尝试发布此代码的新版本作为新答案。
更新
试图获得更干净的东西。您可以使用您喜欢的获取和设置插入符的方式来代替我获得的功能。
我非常感谢对此的任何反馈。
I've been working on something very similar to autocomplete tags. I have some basic code working but I'm a bit ashamed to publish here because it is borderline crap. It needs a lot of cleaning and refactoring which I will get to within the next couple of days. Nevertheless it does what you need in a way similar to what @Andrew suggests.
I will get to this functionality within the next couple of days and will package it as jQuery plugin. I will try to publish a new version of this code as a new answer.
UPDATE
Tried to get something cleaner. You can use your preferred way of getting and setting carets instead of the functions I got.
I would really appreciate any feedback on this.