jQuery Tokeninput 如果不存在则添加
我正在编写一个基于用户输入的脚本,
我有一些字段需要从数据库中查询其值,
如果没有找到条目,我想添加一个新值,以便下一个用户可以通过它找到它自动完成。
我发现这个外观很棒,并且非常漂亮。易于实现名为 TokenInput 的 jquery 插件,但它似乎不
接受我的数据库查询中不可用的条目。
这是该插件的链接: http://loopj.com/jquery-tokeninput/demo.html< /a>
有解决方法吗?或者您是否建议另一个已经具有此功能的插件。
而且我有点担心此类网站的安全方面,在进行此类实现时是否需要注意一些特殊的事情?
I am in the process of writing a script that builds upon user input,
I have some fields that its values need to be quired from the database,
and if no entry found I want to add a new value so the next user will find it through autocomplete.
I found this great looking & easy to implement jquery plugin called TokenInput, but it doesn't seem to
accept entries that are not available in my database query.
Here's the link for the plugin: http://loopj.com/jquery-tokeninput/demo.html
Is there a workaround for this ? Or do you suggest another plugin that already has this feature.
And I'm a little bit concerned about the security aspect of this sort of websites is there something special I need to take care of when doing this sort of implementation ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
现在,这个插件的最新主版本使用allowFreeTagging选项来处理:https://github.com/loopj/jquery-tokeninput/blob/master/src/jquery.tokeninput.js#L57
版本号和文档还没有两年更新一次,所以你必须使用master。
This is now handled in the latest master of this plugin, using the allowFreeTagging option: https://github.com/loopj/jquery-tokeninput/blob/master/src/jquery.tokeninput.js#L57
The version number and docs haven't been updated in 2 years, so you'll have to use master.
当您在字段上启用 tokenInput 时,
该 url 就是 tokenInput 发送搜索查询的位置。它指向一个脚本,该脚本根据与搜索查询匹配的数据库条目返回建议。您想要的是让该脚本在数据库中没有与搜索查询匹配的情况下向列表中添加另一个建议。如何做到这一点很大程度上取决于脚本。
因为您用
php
标记了您的问题,所以我猜测该 url 指向一个 php 脚本,该脚本返回一个充满建议的 JSON 对象。在这种情况下,请修改 php 脚本,以便将新建议添加到列表中:When you enable tokenInput on a field,
that url is where tokenInput sends search queries. It points to a script which returns suggestions based on database entries matching the search query. What you want is to have that script add another suggestion to the list for that case when nothing in your database matches the search query. How to do this depends very much on the script.
Because you tagged your question with
php
, I'm guessing the url points to a php script which returns a JSON object full of suggestions. In that case, modify the php script so that it adds a new suggestion to the list:这是我使用本地 json 的解决方案
id:0 的所有内容都是一个新条目
Here's my solution with local json
Everything with id:0 is a new entry
要添加到 Shawn 的答案(即您需要服务器端的某些内容才能将其实际添加为数据库中的新项目),您可能可以制作 此更改以允许在 javascript 方面添加内容。
To add to Shawn's answer (i.e. you need something server side to actually add it as a new item in the database), you might be able to make this change to allow the addition on the javascript side of things.
我还更改了 onBlur 函数,以便在用户单击搜索框时选择第一个项目 - 这对用户来说更直观:
I also changed the onBlur function to select the first item if they click off the search box - its more intuitive to users:
我在 SteveR 的答案中添加了一些功能,因为我希望即使有结果,该值也显示在下拉列表的顶部。如果所选项目不存在于数据库中,我也想添加它:
I have added some functionalities to SteveR's answer, because I wanted the value to appear at the top of the dropdown even if there are results. Also onAdd if the item selected does not exist in the databese I want to add it:
在 .php 文件中,在 while 条件之前,您可以使用以下内容:
In the .php file, before the while condition, you can use this: