2012-05-02 30 views
1

我想創建jQuery自動完成和PHP,問題是顯示在每條記錄的自動完成有兩個值的數據,我設法做到這一點(無PHP),jquery自動完成與PHP源&json問題

$(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); 
    }; 
}); 

現在我想改變源到PHP頁面(改變作者爲「getauthors.php」)

我不知道該怎麼把裏面的「getauthors.php」爲了通過兩件事標籤和desc。

我設法通過一個這樣的事使用JSON,

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

echo json_encode($json); 
?> 

它輸出這樣的,

autocomplete

你可以看到,第二件事情就是總是不確定的,我怎麼能在PHP中使用json(或任何其他方式)傳遞值。

謝謝。

回答

1
<?php 
$json = array(); 
$json[] = array ("label"=>"test", "desc"=>"description"); 
... 

echo json_encode($json); 
?> 
+0

謝謝,那工作,我知道它的愚蠢的事情,我總是把第二行放在第一個數組。 –