【问题标题】:In rails, jQuery token input is not allowing custom entry在 Rails 中,jQuery 令牌输入不允许自定义输入
【发布时间】:2012-04-17 01:06:26
【问题描述】:

在 Rails 中,jQuery 令牌输入不允许自定义输入。

我已下载 1.6.0 版本的令牌输入,但我无法输入自定义条目。一旦我在文本框中输入一些文本并取出光标,文本就会消失。 只有我需要从自动完成列表中进行选择。

例如:- 如果我使用下面的脚本函数,解决方案是什么?

<pre>
<script type="text/javascript">
tokenInput("SOME_ID", "/token_input/name");

function tokenInput(text_box_id, url){
    jQuery("#" + text_box_id).tokenInput(url, {
      allowCustomEntry: true,
      preventDuplicates: true,
      theme: "facebook",
    });
  }
</script>
</pre>

请给我解决方案,令牌输入应该允许自定义条目。

【问题讨论】:

    标签: jquery jquery-tokeninput


    【解决方案1】:

    它对我很有魅力。

    我需要自定义条目来存储数据库,自动增量 id 将是这个令牌的值。

    我已将其修复如下。有一个技巧,在添加令牌时,请求会发送到服务器,将新令牌添加到数据库,并且它的新插入 ID 会到达客户端并设置为添加令牌的值。

    从 github 获取 plugin 并允许免费标记工具。

    <input type="text" name="w_i_tk" id="w_i_tk">
    
    <script>
    $(document).ready(function() {.
    
        $("#w_i_tk").tokenInput("token.php", {
            theme: "facebook",
            hintText: "Type tag by which other can search, e.g. PHP, MySql etc.",
            preventDuplicates: true,
            tokenLimit: 5,
            minChars: 2,
            onAdd: function (item) {
                if(item.id=="0") {
                    $.ajax({
                        type:"GET",
                        url:"token.php",
                        data:{action:"newtoken",name:item.name},
                        success: function(resp) {
                            $("#w_i_tk").tokenInput("remove", {name: item.name});
                            $("#w_i_tk").tokenInput("add", {id: resp, name: item.name});    
                        }
                    });
    
                }
    
            },
            animateDropdown: false,
            allowFreeTagging: true
        });
    });
    </script>
    

    token.php

    <?php
    if(isset($_GET["q"])) {
        $q = trim($_GET["q"]);
        $isSearchItemExists = false;
        $sql = sprintf("SELECT token_id, token from tokens WHERE token LIKE '%%%s%%' ORDER BY popularity DESC LIMIT 10", mysql_real_escape_string($q));
        $rec = mysql_query($sql);
    
        $arr = array();
        while($row = mysql_fetch_array($rec)) {
            $obj = new stdClass();
            $obj->id = $row["token_id"];
            $obj->name = $row["token"];
            if($obj->name==$q) {
                $isSearchItemExists = true; 
            }
            $arr[] = $obj;
        }
        if(!$isSearchItemExists) $arr = array_merge(getNewToken($q),$arr);
    
        $json_response = json_encode($arr);
    
    
        echo $json_response;
    
    } else if(isset($_GET["action"]) && $_GET["action"]=="newtoken") {
    
        $token = strtolower($_REQUEST["name"]);
        $sql = "SELECT * FROM tokens WHERE token='$token'";
        $rec = mysql_query($sql);
        $numRows = mysql_num_rows($rec);
        if($numRows>0) {
            $row = mysql_fetch_array($rec);
            $id = $row["token_id"]; 
        } else {
            $sql = "INSERT INTO tokens SET token='$token'";
            $rec = mysql_query($sql);
            $id = mysql_insert_id();    
        }
        echo $id;
        exit;
    }
    
    function getNewToken($q) {
    
        $sql = "SELECT max(token_id) as token_id FROM tokens";
        $rec = mysql_query($sql);
        $row = mysql_fetch_array($rec);
        $maxToken = $row["token_id"];
        $newToken = $maxToken + 1;
    
        $newItem = array();
        $new = new stdClass();
        $new->id = "0";
        $new->name = $q;
        $newItem[] = $new;
    
        return $newItem;    
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      这应该很快会在 Tokeninput 主分支中得到修复,但与此同时,将 this branch 合并到您自己的分支中应该会为您解决这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 2013-01-27
        • 2019-05-31
        • 2021-06-15
        • 1970-01-01
        相关资源
        最近更新 更多