php - 远程 JSON 数据源的 JQuery 自动完成功能不起作用

标签 php jquery ajax json autocomplete

我正在使用这个 jquery 插件:JQuery Autocomplete 。问题我正在获取 json 数据,但它没有出现在自动完成列表中。 JQuery 代码:

$( "#student-id" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "ajax/ajax_admin.php?auto_student=" + $( "#student-id" ).val(),
            dataType:"json",
            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.students, function( item ) {
                    return {
                        label: item.id +" , "+ item.name,
                        value: item.id
                    }
                }));
            }
        });
    },
    minLength: 2,
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
});

PHP 脚本是:

public function load_ajax_student_list($val)
{
    $val = "%".$val."%";
    $stmt = $this->conn->prepare("select * from student where studentAiubId like :value limit 0,5");
    $stmt->bindValue(':value', $val);
    $stmt->execute();
    if($stmt->rowCount() < 1)
        echo "";
    else
    {
        $result = $stmt->fetchAll();

        $output = array();
        foreach($result as $row)
        {
            if($row['mname']=="")
                $name = $row['fname']." ".$row['lname'];
            else
                $name = $row['fname']." ".$row['mname']." ".$row['lname'];
            $data["name"] = $name;
            $data["id"] = $row['studentAiubId'];
            $output["students"][] = $data;
        }
        echo json_encode($output);                
    }
}

如果调用如下:ajax/ajax_admin.php?auto_student=10
PHP 脚本 生成的数据是:

{
    "students": [
        {"name":"Moh Mamun Sardar","id":"10-15987-1"},
        {"name":"Rehan Ahmed","id":"10-12451-2"},
        {"name":"Abid Hassan","id":"10-15412-1"},
        {"name":"Abir Islam","id":"10-11245-1"}
    ]
}

但自动完成功能上没有显示任何内容。我做错了什么?

最佳答案

尝试这样的事情

$.map( data.students, function(item ) {
    return {
    label: item.name,
    value: item.id
});

它是minlength而不是minLength见大小写

关于php - 远程 JSON 数据源的 JQuery 自动完成功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21112253/

相关文章:

javascript - 隐藏页面元素然后显示时出错

javascript - 获取或设置 CSS 变量

ajax - GORM持久性事件监听器

PHP xor 返回错误值

php - 在 prestashop 中为 CMS 页面创建新模板

php - 将 ereg_replace 转换为 preg_replace

javascript - 使用 .html() 使用 jQuery 在 src 属性 HTML5 中插入变量

javascript - 类似github的浏览器前进/后退滑动页面效果

php - 使用 jquery ajax 将字符串传递给 php

php - mysql更改时更新页面?