gpt4 book ai didi

javascript - jQuery select2 : duplicate tag getting recreated

转载 作者:行者123 更新时间:2023-12-01 07:47:11 25 4
gpt4 key购买 nike

我今天早些时候问了一个问题 ( jquery select2: error in getting data from php-mysql )。然而,我正在尝试修复它,现在我遇到了一些奇怪的问题。我不知道为什么会这样。

下面是 JavaScript 代码。

<div class="form-group">
<label class="col-sm-4 control-label">Product Name</label>
<div class="col-sm-6">
<input type="hidden" id="tags" style="width: 300px"/>
</div>
</div>

<script type="text/javascript">
var lastResults = [];

$("#tags").select2({
multiple: true,
placeholder: "Please enter tags",
tokenSeparators: [","],
initSelection : function (element, callback) {
var data = [];
$(element.val().split(",")).each(function () {
data.push({id: this, text: this});
});
callback(data);
},
ajax: {
multiple: true,
url: "fetch.php",
dataType: "json",
type: "POST",
data: function(term) {
return {q: term};
},
results: function(data) {
return {results: data};
},

},
createSearchChoice: function (term) {
var text = term + (lastResults.some(function(r) { return r.text == term }) ? "" : " (new)");
return { id: term, text: text };
},
});

$('#tags').on("change", function(e){
if (e.added) {
if (/ \(new\)$/.test(e.added.text)) {
var response = confirm("Do you want to add the new tag "+e.added.id+"?");
if (response == true) {
alert("Will now send new tag to server: " + e.added.id);
/*
$.ajax({
type: "POST",
url: '/someurl&action=addTag',
data: {id: e.added.id, action: add},
error: function () {
alert("error");
}
});
*/
} else {
console.log("Removing the tag");
var selectedTags = $("#tags").select2("val");
var index = selectedTags.indexOf(e.added.id);
selectedTags.splice(index,1);
if (selectedTags.length == 0) {
$("#tags").select2("val","");
} else {
$("#tags").select2("val",selectedTags);
}
}
}
}
});
</script>

这是 php 代码 (fetch.php)

<?php 
// connect to database
require('db.php');

// strip tags may not be the best method for your project to apply extra layer of security but fits needs for this tutorial
$search = strip_tags(trim($_GET['q']));
//$search='te';
// Do Prepared Query
$query = $mysqli->prepare("SELECT tid,tag FROM tag WHERE tag LIKE :search LIMIT 4");

// Add a wildcard search to the search variable
$query->execute(array(':search'=>"%".$search."%"));

// Do a quick fetchall on the results
$list = $query->fetchall(PDO::FETCH_ASSOC);

// Make sure we have a result
if(count($list) > 0){
foreach ($list as $key => $value) {
$data[] = array('id' => $value['tid'], 'text' => $value['tag']);
}
} else {
$data[] = array('id' => '0', 'text' => 'No Products Found');
}

// return the result in json
echo json_encode($data);

?>

select2版本是3.5

上面的代码能够使用 fetch.php 从数据库发送/接收请求。

问题是我的数据库中有两条记录testtemp,当我标记其中任何一个记录时,它会创建新标签。

它应该像这样工作:如果数据库有值,那么它不会创建具有相同名称的新标签。

enter image description here

更新

enter image description here

最佳答案

标签需要一个 ID 和一个文本。您面临的问题是您的文本与 ID 不匹配。

因此,即使您编写相同的文本,Select2 也会认为新文本是新选项,因为 id 不匹配。

要解决您的问题,您需要将 id 设置为与文本相同的值。将 fetch.php 的 foreach 更改为以下内容:

foreach ($list as $key => $value) {
$data[] = array('id' => $value['tag'], 'text' => $value['tag']);
}

更新:您还需要更新变量 lastResults 以避免具有相同文本的标签重复。绑定(bind)select2时,需要将ajaxresults属性改成这样(基于this answer:

ajax: {
multiple: true,
url: "fetch.php",
dataType: "json",
type: "POST",
data: function(term) {
return {q: term};
},
results: function(data) {
lastResults = data.results;
return {results: data};
},
},

注意lastResults = data.results;。如果没有这个,lastResults 变量始终为空,并且当执行 createSearchChoice 函数时,它将始终返回一个新标签。

关于javascript - jQuery select2 : duplicate tag getting recreated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35231584/

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