Jquery plugins jquery tokeninput:如何传递动态数组以进行自动完成

Jquery plugins jquery tokeninput:如何传递动态数组以进行自动完成,jquery-plugins,jquery-tokeninput,Jquery Plugins,Jquery Tokeninput,使用tokenInput,我的自动完成不会返回任何结果,我也不知道为什么 首先,我对数据库进行查询,以查找所连接用户的所有联系人 我得到一个数组,例如: $contacts = Array( [0] => Array ( [id] => 1 [name] => John ) [1] => Array ( [id] => 3 [name] => Peter ) ) 然后我使用http_build_query,因为我想通过URL传递数组: $contacts_u

使用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
然后我使用令牌输入,通过url发送我的数组:

$(document).ready(function () {
    $("#my-text-input").tokenInput("<?php echo $this->webroot.'populate.php?'.$contacts_url ?>", {theme: "facebook"});
});
我觉得看起来不错

但自动完成不会返回任何结果:(

非常感谢任何帮助


非常感谢

默认情况下,search参数也是作为
$GET
变量发送的,我想这会弄乱您的JSON编码

一种更好的方法是在本地进行,如下所示:

$(document).ready(function () {
    $("#my-text-input").tokenInput("<?php echo json_encode($contacts) ?>", {theme: "facebook"});
});
$(文档).ready(函数(){
$(“#我的文本输入”).tokenInput(“,{主题:“facebook”});
});

您的搜索参数也作为get变量发送,我想这会弄乱您的json_编码。您是否尝试过使用类似Chrome的web工具来查看服务器对键入字符的响应?如果您可以获得一个实时演示,这将非常有助于正确查看发生了什么。另外,是否有特殊原因ar为什么你要从PHP发送所有联系人,打印到Javascript,然后再发送回PHP?这是一种奇怪的方式!如果这就是你所做的,我不明白你为什么要从服务器请求,而服务器不打印到本地JSON数组-jQuery TokenInput也可以使用它。非常感谢Chris!!你说对了两次!我的url很混乱,在通过tokeninput()后,它将%更改为%25,并在末尾添加了&q=a。但是您的第二条评论非常有价值!我删除了此php文件,并执行了$json_string=json_encode($contacts);$(document)。ready(函数(){$(“#我的文本输入”)。tokeninput(,{theme:“facebook”});});现在一切都正常了。非常感谢您的帮助!虽然这段代码可能会回答这个问题,但提供有关这段代码为什么和/或如何回答这个问题的附加上下文可以提高其长期价值。
[{"id":"1","name":"John"},{"id":"3","name":"Peter"}]
$(document).ready(function () {
    $("#my-text-input").tokenInput("<?php echo json_encode($contacts) ?>", {theme: "facebook"});
});
$(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                    
            });
    });      
$('#product_tokens').tokenInput('/products.json', { propertyToSearch: 'product' });