0
<script type="text/javascript" src="js/jquery.js"></script>
<script type='text/javascript' src='js/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />

自动完成在页面刷新时第一次工作,如果我退格并从开始输入其他字符,我最多输入 4 个字符,它不起作用,有人可以帮我为什么会发生这种情况

$().ready(function() {


    $("#customername").autocomplete("ajax/customerlist.php", {
        width: 260,
        dataType: 'json',
        matchContains: false,
        selectFirst: false,
        minlength:4,
        parse: function(data) {

                var array = new Array();
                for(var i=0;i<data.items.length;i++)
                {       //alert(data.items[i].name);
                        array[array.length] = { data: data.items[i], value: data.items[i], result: data.items[i].name };
                }
                return array;
        },
        formatItem: function(row) {                     
                var name = '';
                if (row.first_name && row.last_name)
                        name = '('+row.first_name+', '+row.last_name+')';
                else if (row.first_name)
                        name = '('+row.first_name+')';
                else if (row.last_name)
                        name = '('+row.last_name+')';

                return row.name+' '+name;
        }
    });

    $('#customername').result(function (event, data, formatted) {
      $( "#cust" ).val( data.id );
      var add= data.add1+data.add2;
      $( "#address" ).val( add );
      $( "#email" ).val( data.email );
      $( "#phone" ).val( data.phone );
      $( "#mobile" ).val( data.mobile );

    });
});

php代码是

$getuser=$db->execute("select * from `customer` where customer_name like '%".mysql_$_REQUEST['q']."%'  ");


while($getuserlist = $db->fetchAll($getuser)) {

$list['items'][]=array('name' => $getuserlist['customer_name'],'id'=>$getuserlist['customer_id'],'add1'=>$getuserlist['add1'],'add2'=>$getuserlist['add2'],'city'=>$getuserlist['city'],'state'=>$getuserlist['state'],'pincode'=>$getuserlist['pincode'],'email'=>$getuserlist['email'],'phone'=>$getuserlist['phone'],'mobile'=>$getuserlist['mobile']);

}

echo json_encode($list);
4

1 回答 1

2

改变这个

$().ready(function() {

$("#customername").keyup(function() {

它会在你输入时运行"#customername"

于 2012-10-08T06:45:18.917 回答