我想使用 jqueryUi 的自动完成功能将搜索限制为名称,以便返回的结果只是名称,不包括其他数组值,如传真和其他内容。
这是我的 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);
?>
爪哇语
$(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);
}
});
谢谢。
当source
是对象数组时 - [ { label: "Choice1", value: "value1" }, ... ]
,只有label
(如果未提供label
,则value
)在建议菜单中搜索/显示 http://api.jqueryui.com/autocomplete/#option-source。因此,您可以在返回的数组中包含 fax
、founder
和其他键/值集,并且它们不会被搜索/显示。
另一种方法是,您只获取 php 脚本中的名称,然后在select
执行 ajax 请求以获取所有其他数据。
注意:您正在使用ui.item.name
-
$('#ProName' ).html(ui.item.name);
但是name
不在数组中,所以它应该label
$('#ProName' ).html(ui.item.label);