2013-10-31 33 views
0

我想限制搜索名稱使用jqueryUi的自動完成,以便返回的結果只有名稱,不包括其他數組值,如傳真和東西。jquery-ui自動完成只允許從多個json結果名稱搜索

這是我的php。

<?php 
require_once 'db_conx.php'; 
$req = "SELECT * " 
    ."FROM profiles " 
    ."WHERE name LIKE '%".mysql_real_escape_string($_REQUEST['term']) ."%' "; 
$query = mysql_query($req); 
while($row = mysql_fetch_array($query)) 
{ 
    $return = array ('label' => $row['name'], 
    'founder' => $row['founder'], 
    'fax' => $row['fax'] 

    ); 

} 
echo json_encode($return); 
?> 

的Javascript

$(function() { 
     $("#SearchInput").autocomplete({ 
      source: '../Search/BP_Search.php', 
      minLength: 2, 
      autoFocus: false, 
      select: function(event, ui) { 
      $('#ProName').html(ui.item.name); 
      $('#ProFax').html(ui.item.fax); 
      $('#ProFounder').html(ui.item.founder); 
      } 
    }); 

感謝。

+0

從腳本刪除它們? – Krishna

回答

0

source是對象的數組 - [ { label: "Choice1", value: "value1" }, ... ],只有label(或value如果label不設置)被搜索/在建議菜單http://api.jqueryui.com/autocomplete/#option-source顯示。因此,您可以在返回的數組中使用faxfounder和其他鍵/值集,並且不會搜索/顯示它們。

另一種方法是,您只能在您的php腳本中獲取名稱,然後在select上執行ajax請求以獲取所有其他數據。


注意:您正在使用ui.item.name - 在不

$('#ProName').html(ui.item.name); 

name你的陣中,所以它應該是label

$('#ProName').html(ui.item.label);