duanci3845 2012-05-02 14:57
浏览 78
已采纳

jquery autocomplete与php source&json问题

I want to create jquery autocomplete and php, the problem is the data shown in each record of the autocomplete has two values, I managed to do this like this (without PHP),

$(function() {
        var authors = [
            {
                label: "jQuery",
                desc: "the write less, do more, JavaScript library"
            },
            {
                label: "jQuery UI",
                desc: "the official user interface library for jQuery"
            },
            {
                label: "Sizzle JS",
                desc: "a pure-JavaScript CSS selector engine"
            }
        ];

    $( "#authorname" ).autocomplete({
        minLength: 0,
        source: authors,
        focus: function( event, ui ) {
            $( "#authorname" ).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $( "#authorname" ).val( ui.item.label );
            return false;
        }
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
            .appendTo( ul );
    };
});

now I want to change the source to php page (change authors to "getauthors.php")

I don't know what to put inside "getauthors.php" in order to pass two things for label and desc.

I managed to pass one thing like this using json,

<?php
$json = array();
$json[]="test";
$json[]="test2";
$json[]="test3";

echo json_encode($json);
?>

which outputs like this,

autocomplete

you can see that the second thing is always undefined, how I can pass value to this in php using json (or any other way).

Thanks.

  • 写回答

1条回答 默认 最新

  • douji5746 2012-05-02 15:02
    关注
    <?php
    $json = array();
    $json[] = array ("label"=>"test", "desc"=>"description");
    ...
    
    echo json_encode($json);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理