2012-10-09 16 views
0

基本上我得到一個PHP文件,它返回一個不同結果的數組(PHP發送數組並且收到的很好)。現在我希望JQuery自動完成只能通過字段「Name」而不是其他字段進行搜索,並且在提示結果時它只顯示「Name」字段。但一旦被選中,我希望它在頁面中放入所有結果數組。Json + Jquery自動完成+ php:當Json有多個項目時選擇搜索選項中顯示的信息

目前它顯示的是在建議的搜索中拉取所有數組的所有內容,並且只顯示HTML中的'name'。

我想我要做的就是趕上在JQ變量,然後搜索只是名字

誰能幫助我PLZ? THXü

PHP(工程)

   foreach ($this->get_charities() as $r){ 

         $return = array ( 'Name' => $r['Name'], 
              'Contact' => $r['Contact'], 
              'Postcode' => $r['Postcode'], 
              'Phone' => $r['Phone'], 
              'Fax' => $r['Fax'], 
              'Website' => $r['Website'],        
              'Email' => $r['Email'], 
             ); 
       } 

     return json_encode($return);  

JS

<script> 
     $(function() { 
      function log(message) { 
       $("<div>").text(message).prependTo("#log"); 
       $("#log").scrollTop(0); 
      } 

      $("#birds").autocomplete({ 
       source: "search.php", 
       minLength: 2, 
       select: function(event, ui) { 
        log(ui.item ? 
         "Selected: " + ui.item.value + " aka " + ui.item.id : 
         "Nothing selected, input was " + this.value); 
       } 
      }); 
     }); 
     </script> 

回答

0

你需要設置PHP數組如下

$return = array ( 
    'value'=> $r['Name'], 
    'label'=> $r['Name'], 
    'Contact' => $r['Contact'], 
    'Postcode' => $r['Postcode'], 
    'Phone' => $r['Phone'], 
    'Fax' => $r['Fax'], 
    'Website' => $r['Website'],        
    'Email' => $r['Email'], 
); 

現在,自動完成僅在名稱值搜索並且您還可以將您的自定義值導入選擇功能

$("#birds").autocomplete({ 
    source: "search.php", 
    minLength: 2, 
    select: function(event, ui) { 
      alert(ui.item.Phone); <-- here you can get all your custom values. 
     alert(ui.item.Contact); <-- here you can get all your custom values. 

     log(ui.item ? 
      "Selected: " + ui.item.value + " aka " + ui.item.id : 
      "Nothing selected, input was " + this.value); 
    } 
}); 
+0

它說undefined在警報(ui.Phone) –

+0

哦..對不起。這是我的錯誤,應該警惕(ui.item.Phone)。編輯我的回答 – GBD

+0

@JonathanThurft你有沒有嘗試我編輯的新答案? – GBD

相關問題