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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM