2

我有这个自动完成 php 功能:

搜索.php

if ( !isset($_REQUEST['term']) )
    exit;

$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('inb');

$rs = mysql_query('select name, l_name, level from pacient 
            where name like "'. mysql_real_escape_string($_REQUEST['term']) .'%"
            or l_name like "'. mysql_real_escape_string($_REQUEST['term']) .'%"
            order by name asc limit 0,10', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $nivel = $row['nivel_nivel'];
        $data[] = array(
            'label' => $row['name'].' '. $row['l_name'].' '.$row['level'],
            'value' => $row['name'].' '. $row['l_name'].' '.$row['level']
        );
    }
}
echo json_encode($data);
flush();
?>

我将其称为 html 主文件:

jQuery(document).ready(function(){ 
            $('#nameField').autocomplete({source:'search.php', minLength:1});
        });

现在,我想获取在 php 文件中进行的查询的值,并将其放在 html 文件的输入字段中。知道我该怎么做吗?

4

1 回答 1

0

response您可以在 JS 自动完成定义中为该方法设置一个侦听器。您需要更改您的 PHP 以在该数据集中的某处包含您想要的新值。

像这样的东西:

$data[] = array(
        'label' => $row['name'].' '. $row['l_name'].' '.$row['level'],
        'value' => $row['name'].' '. $row['l_name'].' '.$row['level'],
        'foo' => <WHATEVER VALUE YOU WANT HERE>
    );

然后在 jQuery 中:

$('#nameField').autocomplete({
    source:'search.php', 
    minLength:1,
    response : function(event, data) {
        data.content[0].foo; //this will have your foo value, do whatever you want with it here
    }
});
于 2013-03-19T21:09:27.300 回答