【问题标题】:Array format for autocomplete JQuery with JSON使用 JSON 自动完成 JQuery 的数组格式
【发布时间】:2014-01-16 09:05:46
【问题描述】:

我正在尝试更改我的自动完成 JQuery 的源数据,但我无法理解我必须在我的 JS 函数中以 PHP 发送什么样的数组,因为我使用它来更改源数据:

$( "#autocomplete" ).autocomplete('option', 'source', tab);

然后我将此结果发送到我的 JS 函数:

{"transport":{"voiture":"voiture","car":"car","avion":"avion"}}

我的 php 对于这个结果:

foreach ($result as $k => $v)  {        
    //Stockage des valeurs dans un tableau associatif 
        $listtransport[$v['transport']] = $v['transport'];
    }

提前致谢!

【问题讨论】:

标签: javascript php jquery json jquery-ui-autocomplete


【解决方案1】:

这是 JqueryUI 自动完成接受的 JSON

[
    { 
      id:    elementId, 
      label: label,
      value: value to set when the element is selected
    },
    //Write other elements
]

您可以按照以下方式构建此数组

$ayValues = array();

foreach ($result as $k => $v) {
    $ayValues[] = array(
        'id' => $v['transport']['id index'],         //This is the id of the value
        'label' => $v['transport']['label index'],   //This is the label show
        'value' => $v['transport']['value index']    //This is the value setted
    );
}

print json_encode($ayValues);

如果没有 $result 的内容,就不可能写出清晰的回复。

你可以省略 id 键

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多