JQuery autocomplete 结合ajax完美应用

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title>jQuery Autocomplete Plugin</title>
<script type="text/javascript" src="../jquery_autocomplete/lib/jquery.js"></script>
<script type='text/javascript' src='../jquery_autocomplete/lib/jquery.bgiframe.min.js'></script>
<script type='text/javascript' src='../jquery_autocomplete/lib/jquery.ajaxQueue.js'></script>
<script type='text/javascript' src='../jquery_autocomplete/jquery.autocomplete.js'></script>
<script type='text/javascript' src='../jquery_autocomplete/lib/thickbox-compressed.js'></script>

<link rel="stylesheet" type="text/css" href="../jquery_autocomplete/jquery.autocomplete.css" />

    
<script type="text/javascript">
$().ready(function() {

    
    $("#suggest1").autocomplete("ajax.php", {
        minChars: 0,
        width: 230,
        highlight: false,
        scrollHeight: 300,
        matchContains: "word",  
        autoFill: false,  
        parse: function(data) {
            return $.map(eval(data), function(row) {
                return {
                    data: row,
                    value: row.name,
                    result: row.to
                }
            }); //对ajax页面传过来的数据进行json转码
        },
        formatItem: function(row, i, max) {
          return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
          },
        formatMatch: function(row, i, max) {
         return row.name + row.to;
        },
         formatResult: function(row) {
          return row.to;
        }
    }).result(function(event, row, formatted) {
                    return row.to;
             });
    

});


</script>
    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

<h1 id="banner"><a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete Plugin</a> local deomo</h1>

<div id="content">
    
    <form>
        <p>
            <label>选择客户:</label>
            <input type="text" id="suggest1" size="30px"/>
            
        </p>
        
    </form>
</div>

</body>
</html>

 index结束

 

ajax.php

 

<?php

$conn = @mysql_connect("localhost","root","") or die("Failed!");
mysql_select_db("jquery_test",$conn);
mysql_query("set names 'utf8'");

$q = $_GET["q"];
if (!$q) return;

$items = array();
$sql1 = mysql_query("select `CLIENT_N_SHORT`,`CLIENT_NAME` from `client`");
while($row = mysql_fetch_array($sql1)){
    $key = $row['CLIENT_N_SHORT'];
    $value = $row['CLIENT_NAME'];
    
    if (strpos($value, $q) !== false) {
        array_push($items, array(
            "name" => urlencode($key),
            "to" => urlencode($value)
        ));

//防止json中文乱码 因此转码
    
    }
}
//print_r($items);
//exit();
//echo "<hr>";

echo urldecode( json_encode($items) );
?>

有什么不懂的可以追问

原创精品,转载请说明出处http://www.cnblogs.com/jia58960/archive/2012/02/22/2362875.html

转载于:https://www.cnblogs.com/jia58960/archive/2012/02/22/2362875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值