【问题标题】:JQuery UI autocomplete with json and ajax带有 json 和 ajax 的 JQuery UI 自动完成
【发布时间】:2012-08-04 20:10:46
【问题描述】:

我看到很多关于通过 JSON 传递带有标签和值属性的数组的问题,但关于传递字符串的问题不多。我的问题是我似乎无法填充我的自动完成功能。我运行了一个转储函数,并将这些示例值通过 JSON 传递给自动完成:

0: 23456
1: 21111
2: 25698

这里有一些代码:

$("#auto_id").autocomplete( {
    source: function(request,response) {

        $.ajax ( {
          url: "fill_id.php",
          data: {term: request.term},
          dataType: "json",
          success: function(data) {
            //what goes here?
                 } 
    }) }
   });

这里是fill_id.php:

$param = $_GET['term'];
$options = array();


$db = new SQLite3('database/main.db');
    $results = $db->query("SELECT distinct(turninId) FROM main WHERE turninid LIKE '".$param."%'");


while ($row_id = $results->fetchArray()) {
        $options[] =  $row_id['turninId']; 
    }   
echo json_encode($options);

我的自动完成功能保持空白。如何更改我的 JSON 数组以填充它?或者我在我的 ajax 成功函数中包含什么?

【问题讨论】:

    标签: php jquery ajax json autocomplete


    【解决方案1】:

    您可以非常坚持 jQuery UI 的自动完成的远程演示:http://jqueryui.com/resources/demos/autocomplete/remote-jsonp.html

    要将您的结果放入自动完成列表中,您需要在您的 ajax 成功函数中放置一个带有标签和值的对象到响应参数(实际上是一个函数):

    source: function( request, response ) {
        $.ajax({
            url: "fill_id.php",
            data: {term: request.term},
            dataType: "json",
            success: function( data ) {
                response( $.map( data.myData, function( item ) {
                    return {
                        label: item.title,
                        value: item.turninId
                    }
                }));
            }
        });
    }
    

    但这只有在你稍微修改一下你的 fill_id.php 时才会起作用:

    // escape your parameters to prevent sql injection
    $param   = mysql_real_escape_string($_GET['term']);
    $options = array();
    
    // fetch a title for a better user experience maybe..
    $db = new SQLite3('database/main.db');
        $results = $db->query("SELECT distinct(turninId), title FROM main WHERE turninid LIKE '".$param."%'");
    
    while ($row_id = $results->fetchArray()) {
        // more structure in data allows an easier processing
        $options['myData'][] = array(
            'turninId' => $row_id['turninId'],
            'title'    => $row_id['title']
        ); 
    }
    
    // modify your http header to json, to help browsers to naturally handle your response with
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    echo json_encode($options);
    

    当然,当您的表格中没有标题或任何内容时,您也可以保持原样回复并在成功回调中重复 id。重要的是,您在自动完成中使用值/项目对填充 response 函数:

    // this will probably work without modifying your php file at all:
    response( $.map( data, function( item ) {
        return {
            label: item,
            value: item
        }
    }));
    

    编辑: 更新了新 jquery UI 的自动完成 ui 的参考链接

    【讨论】:

    • 我以为你也可以只用字符串填充数组?
    • 另外,我的http需要修改吗?你能解释一下吗?
    • 感谢您的帮助!完美运行!
    • http头的修改不是必须的,因为jquery可以自己检测数据类型。但它提高了性能,因为它有助于现代浏览器自然地实现 json 处理。所以这项工作可以通过你的浏览器来完成,它速度更快,需要更少的内存,而不是 jquery 在 javascript 中的实现
    • 我和你在这里提到的一样,但是我的自动完成没有弹出文本...
    猜你喜欢
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多