jquery-ui 自动完成仅允许从多个 JSON 结果中按名称搜索

jquery-ui auto-complete only allow search by name from multiple json results

本文关键字:结果 JSON 搜索 jquery-ui      更新时间:2023-09-26

我想使用 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。因此,您可以在返回的数组中包含 faxfounder 和其他键/值集,并且它们不会被搜索/显示。

另一种方法是,您只获取 php 脚本中的名称,然后在select执行 ajax 请求以获取所有其他数据。


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

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

但是name不在数组中,所以它应该label

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