gpt4 book ai didi

jquery - 在 Rails 中,jQuery token 输入不允许自定义输入

转载 作者:行者123 更新时间:2023-12-01 01:37:21 27 4
gpt4 key购买 nike

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

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

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

<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>

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

最佳答案

它对我来说很有魅力。

我需要自定义条目来存储数据库,并且自动增量 id 将是该 token 的值。

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

获取plugin允许来自 github 的免费标记工具。

<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;
}
?>

关于jquery - 在 Rails 中,jQuery token 输入不允许自定义输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9992231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com