gpt4 book ai didi

php - 将 json 对象传递给数组 - 自动完成

转载 作者:行者123 更新时间:2023-11-28 13:56:23 26 4
gpt4 key购买 nike

我有两个文件。

location.php, that outputs this:

[["javascript"],["PHP"]]

在另一个文件中:

<script type="text/javascript">
$.getJSON('location.php', function(data) {
var sampleTags = [];

$.each(data, function(key, val) {
sampleTags.push(val);
});

alert(sampleTags); // show javascript, php


//-------------------------------
// Preloading data in markup
//-------------------------------
$('#myULTags').tagit({
availableTags : sampleTags, // this param is of course optional. it's for autocomplete.
// configure the name of the input field (will be submitted with form), default: item[tags]
itemName : 'item',
fieldName : 'tags'
});
});
</script>

自动完成功能不起作用。为什么?

如果我使用:

var sampleTags = [ 'javascript', 'php'];

一切都运行良好,但使用 json 时,自动完成功能根本不起作用。

最佳答案

$.each(data, function(key, val) {
sampleTags.push(val[0]);
});

应将 [["foo"],["bar"]] 减少为 ["foo", "bar"]

关于php - 将 json 对象传递给数组 - 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7716262/

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