使用tokenInput,我的自动完成不返回任何东西,我也不知道为什么。
首先,我对数据库进行查询,以查找连接用户的所有联系人。
例如,我得到了一个数组:
$contacts = Array( [0] => Array ( [id] => 1 [name] => John ) [1] => Array ( [id] => 3 [name] => Peter ) )然后,我使用http_build_query,因为我想通过URL传递数组:
$contacts_url =http_build_query($contacts);返回:
print_r($contacts_url)= 0%5Bid%5D=1&0%5Bname%5D=John&1%5Bid%5D=3&1%5Bname%5D=Peter然后使用tokenInputs,并通过url发送数组:
$(document).ready(function () {
$("#my-text-input").tokenInput("<?php echo $this->webroot.'populate.php?'.$contacts_url ?>", {theme: "facebook"});
});populate.php页面包含:
<?php
header('Content-type: text/javascript');
$a= $_GET;
$json_string = json_encode($a);
echo $json_string;
?>如果我打开php页面../populate.php?0%5Bid%5D=1&0%5Bname%5D=John&1%5Bid%5D=3&1%5Bname%5D=Peter,我会看到:
[{"id":"1","name":"John"},{"id":"3","name":"Peter"}]我觉得看起来不错
但是,自动完成不返回任何内容:(
任何帮助都非常感谢!
非常感谢
发布于 2015-01-15 15:43:04
默认情况下,搜索参数也是作为$GET变量发送的,我认为这会扰乱您的JSON代码。
一种更好的办法是在当地这样做,如下所示:
$(document).ready(function () {
$("#my-text-input").tokenInput("<?php echo json_encode($contacts) ?>", {theme: "facebook"});
});发布于 2015-08-21 16:51:23
$(document).ready(function() {
$("#searchword").tokenInput(
/* Given that $json_res = json_encode($user_arr); in the PHP.
Using <?php echo $json_res ?> OR <?php echo json_encode($user_arr) ?> without quotes and blocks gives the same result.
The other way is to define var ar =<?php echo $json_res?>; in JS and pass it as .tokenInput(ar, {} */
<?php echo $json_res ?>
, {
propertyToSearch: "username",
theme: "facebook",
preventDuplicates: true,
excludeCurrent: true
});
}); //如果JSON文件具有{"id":"856“、”产品“:”sample1“}、{"id":"1035”、“产品”:“示例产品2"},请执行以下操作:
$('#product_tokens').tokenInput('/products.json', { propertyToSearch: 'product' });https://stackoverflow.com/questions/27932481
复制相似问题