jQuery Tokeninput 如果不存在则添加

发布于 2024-12-06 10:41:16 字数 438 浏览 0 评论 0原文

我正在编写一个基于用户输入的脚本,

我有一些字段需要从数据库中查询其值,

如果没有找到条目,我想添加一个新值,以便下一个用户可以通过它找到它自动完成。

我发现这个外观很棒,并且非常漂亮。易于实现名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

北凤男飞 2024-12-13 10:41:16

现在,这个插件的最新主版本使用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.

心如狂蝶 2024-12-13 10:41:16

当您在字段上启用 tokenInput 时,

$(selector).tokenInput(url, ...

该 url 就是 tokenInput 发送搜索查询的位置。它指向一个脚本,该脚本根据与搜索查询匹配的数据库条目返回建议。您想要的是让该脚本在数据库中没有与搜索查询匹配的情况下向列表中添加另一个建议。如何做到这一点很大程度上取决于脚本。

因为您用 php 标记了您的问题,所以我猜测该 url 指向一个 php 脚本,该脚本返回一个充满建议的 JSON 对象。在这种情况下,请修改 php 脚本,以便将建议添加到列表中:

"{id: " . $idForThisNewSuggestion . ", name: \"" . $searchQueryString . " (new suggestion)\"}"

When you enable tokenInput on a field,

$(selector).tokenInput(url, ...

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:

"{id: " . $idForThisNewSuggestion . ", name: \"" . $searchQueryString . " (new suggestion)\"}"
沫雨熙 2024-12-13 10:41:16

这是我使用本地 json 的解决方案

$("#input").tokenInput(yourjsondata,{
    preventDuplicates: false,
    onResult: function (item) {
        if($.isEmptyObject(item)){
              return [{id:'0',name: $("tester").text()}]
        }else{
              return item
        }

    },
});

id:0 的所有内容都是一个新条目

Here's my solution with local json

$("#input").tokenInput(yourjsondata,{
    preventDuplicates: false,
    onResult: function (item) {
        if($.isEmptyObject(item)){
              return [{id:'0',name: $("tester").text()}]
        }else{
              return item
        }

    },
});

Everything with id:0 is a new entry

南街九尾狐 2024-12-13 10:41:16

要添加到 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.

清泪尽 2024-12-13 10:41:16

我还更改了 onBlur 函数,以便在用户单击搜索框时选择第一个项目 - 这对用户来说更直观:

.blur(function () {
        $(this).val("");
        if ($(".token-input-selected-dropdown-item").length>0)
            add_token($(".token-input-selected-dropdown-item").data("tokeninput"));
        hide_dropdown();
    })

I also changed the onBlur function to select the first item if they click off the search box - its more intuitive to users:

.blur(function () {
        $(this).val("");
        if ($(".token-input-selected-dropdown-item").length>0)
            add_token($(".token-input-selected-dropdown-item").data("tokeninput"));
        hide_dropdown();
    })
執念 2024-12-13 10:41:16
 $(document).ready(function() {
            $("#expert").tokenInput("source.php", {
                theme: "facebook",
                noResultsText:'Skill Not Found - Enter to Add',
                queryParam:'q',
                onResult: function (item) {
                                            if($.isEmptyObject(item)){
                                                  return [{id:$("#token-input-expert").val(),name: $("#token-input-expert").val()+'*'}]
                                            }else{
                                                  return item
                                            }
                },
                preventDuplicates:true<?php if($expert_rank_json){?>,
                prePopulate: <?php echo $expert_rank_json ?>
                <?php } ?>
            });
 $(document).ready(function() {
            $("#expert").tokenInput("source.php", {
                theme: "facebook",
                noResultsText:'Skill Not Found - Enter to Add',
                queryParam:'q',
                onResult: function (item) {
                                            if($.isEmptyObject(item)){
                                                  return [{id:$("#token-input-expert").val(),name: $("#token-input-expert").val()+'*'}]
                                            }else{
                                                  return item
                                            }
                },
                preventDuplicates:true<?php if($expert_rank_json){?>,
                prePopulate: <?php echo $expert_rank_json ?>
                <?php } ?>
            });
老街孤人 2024-12-13 10:41:16

我在 SteveR 的答案中添加了一些功能,因为我希望即使有结果,该值也显示在下拉列表的顶部。如果所选项目不存在于数据库中,我也想添加它:

 $("#my_input").tokenInput(my_results_route), {
    hintText: "Select labels",
    noResultsText: "No results",
    searchingText: "Searching...",
    preventDuplicates: true,
    onResult: function(item) {
        if($.isEmptyObject(item)){
            return [{id:'0', name: $("tester").text()}];   
        } else {
            //add the item at the top of the dropdown
            item.unshift({id:'0', name: $("tester").text()});
            return item; 
        }
    },
    onAdd: function(item) {
        //add the new label into the database
        if(!parseInt(item.id)) {
            //database insertion ajax call
            console.log('Add to database');
        }
    }
});

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:

 $("#my_input").tokenInput(my_results_route), {
    hintText: "Select labels",
    noResultsText: "No results",
    searchingText: "Searching...",
    preventDuplicates: true,
    onResult: function(item) {
        if($.isEmptyObject(item)){
            return [{id:'0', name: $("tester").text()}];   
        } else {
            //add the item at the top of the dropdown
            item.unshift({id:'0', name: $("tester").text()});
            return item; 
        }
    },
    onAdd: function(item) {
        //add the new label into the database
        if(!parseInt(item.id)) {
            //database insertion ajax call
            console.log('Add to database');
        }
    }
});
恏ㄋ傷疤忘ㄋ疼 2024-12-13 10:41:16

在 .php 文件中,在 while 条件之前,您可以使用以下内容:

array_push($yourarray, array('id'=> 0 ,'name'=> $_GET["term"])); 

In the .php file, before the while condition, you can use this:

array_push($yourarray, array('id'=> 0 ,'name'=> $_GET["term"])); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文