jquery UI 自动完成错误 - 无法使用 'in' 运算符在 [myarray] 中搜索“103”
jquery UI autocomplete error - Cannot use 'in' operator to search for '103' in [myarray]
我正在尝试使用 jquery-ui 创建自动完成功能。
我需要从数据库中获取一些标签,为此我正在使用以下代码:
autocomplete.php
$db = new mysqli('localhost', 'user', 'pass', 'db');
if ($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
$all_tags = array();
$query = "select keyword from nickwebd_web_res.resources limit 0,10";
$result = mysqli_query($db, $query) or die(mysqli_error($db));
while ($row = $result->fetch_assoc()) {
$tags = $row['keyword'];
$row_tags = explode(", ", $tags);
$all_tags = array_merge($all_tags, $row_tags);
}
$all_tags = array_unique($all_tags);
sort($all_tags);
$encodedData = json_encode($all_tags);
echo json_encode($encodedData);
来自 js:
var tagInput = $('#keywords');
tagInput.tagEditor({
delimiter: ', ',
autocomplete: {
source: "autocomplete.php"
}
});
但是如果我尝试输入,我会得到这个错误:
jquery.js: Uncaught TypeError: Cannot use 'in' operator to search for
'103' in ["background","color","test"]
自动完成在这里使用这个插件:https://goodies.pixabay.com/jquery/tag-editor/demo.html
我做错了什么?
感谢您的帮助
我解决了。这可能不是最好的解决方案,但它确实有效,我可以接受它,因为它是用于个人项目的。
php 文件的更改如下:
$encodedData = json_encode($all_tags);
在 javascript 中,我需要一次包含上述变量的 php 文件,如下所示:
<?php require_once ('autocomplete.php') ?>
然后我创建如下变量:
var localSource = <?php echo $encodedData ?>;
最后我使用这个变量作为自动完成的来源:
tagInput.tagEditor({
delimiter: ', ',
autocomplete: {
source: localSource,
position: { collision: 'flip' },
minLength: 2
}
});
现在一切正常。
我正在尝试使用 jquery-ui 创建自动完成功能。
我需要从数据库中获取一些标签,为此我正在使用以下代码:
autocomplete.php
$db = new mysqli('localhost', 'user', 'pass', 'db');
if ($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
$all_tags = array();
$query = "select keyword from nickwebd_web_res.resources limit 0,10";
$result = mysqli_query($db, $query) or die(mysqli_error($db));
while ($row = $result->fetch_assoc()) {
$tags = $row['keyword'];
$row_tags = explode(", ", $tags);
$all_tags = array_merge($all_tags, $row_tags);
}
$all_tags = array_unique($all_tags);
sort($all_tags);
$encodedData = json_encode($all_tags);
echo json_encode($encodedData);
来自 js:
var tagInput = $('#keywords');
tagInput.tagEditor({
delimiter: ', ',
autocomplete: {
source: "autocomplete.php"
}
});
但是如果我尝试输入,我会得到这个错误:
jquery.js: Uncaught TypeError: Cannot use 'in' operator to search for '103' in ["background","color","test"]
自动完成在这里使用这个插件:https://goodies.pixabay.com/jquery/tag-editor/demo.html
我做错了什么?
感谢您的帮助
我解决了。这可能不是最好的解决方案,但它确实有效,我可以接受它,因为它是用于个人项目的。
php 文件的更改如下:
$encodedData = json_encode($all_tags);
在 javascript 中,我需要一次包含上述变量的 php 文件,如下所示:
<?php require_once ('autocomplete.php') ?>
然后我创建如下变量:
var localSource = <?php echo $encodedData ?>;
最后我使用这个变量作为自动完成的来源:
tagInput.tagEditor({
delimiter: ', ',
autocomplete: {
source: localSource,
position: { collision: 'flip' },
minLength: 2
}
});
现在一切正常。