求一个当用户在input对象输入时,能自动从数据库寻找匹配的自动提示的源代码,昨天找几个用jquery的,不过不支持中文,不知道那位大侠有支持中文的,能分享下,非常感谢!!

解决方案 »

  1.   

    你用jquery不支持中文是因为你的编码有问题啊!!1
      

  2.   

    那你也要看jquery中ajax返回的数据的编码啊
      

  3.   

    不会ajax啊,怎么看?怎么改啊?
      

  4.   

    用用的是这个代码
    <script src="jquery-1.2.1.pack.js" type="text/javascript" charset="gb2312"></script>
    <script type="text/javascript">
    function lookup(inputString) {    if(inputString.length == 0) {
            // Hide the suggestion box.
            $('#suggestions').hide();
        } else {
            $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);}
                    else {
                    $('#suggestions').hide();}
                
            });
        }
    } // lookup function fill(thisValue) {
        $('#inputString').val(thisValue);
       $('#suggestions').hide();
    } </script><?php
    $db = mysql_connect("127.0.0.1:3307", "root","123");
        if(!$db) {
        // Show error if we cannot connect.
        echo 'ERROR: Could not connect to the database.';
    } else {
        // Is there a posted query string?
        if(isset($_POST['queryString'])) {
            $queryString = $_POST['queryString'];
            $length=strlen($queryString);
    $sql="SELECT title FROM groups WHERE title LIKE '$queryString%' LIMIT 10";
    mysql_select_db("qq",$db);
    $result = mysql_query("set names gb2312");        // Is the string length greater than 0?
          if($length>0) {
                $query = mysql_query($sql);
            // Run the query: We use LIKE ‘$queryString%’
                if($query) {
                while ($myrow = mysql_fetch_array($query))  {
                    // Format the results, im using <li> for the list, you can change it.         
                    // The onClick function fills the textbox with the result.
                    echo '<li onclick="fill(\''.$myrow[0].'\');">'.$myrow[0].'</li>'; 
                            }
                } else {
                echo "ERROR: There was a problem with the query $sql.";
                    }
            } else {
                 }
        } else {
        echo 'There should be no direct access to this script!';
        }
    }
    ?>
    <div>
        <input type="text" name="searchvalue" size="30" value="<?php echo $searchvalue ?>" id="inputString" oninput="lookup(this.value);"onkeyup="lookup(this.value);">
    <input type="Submit" name="search" value="搜索">
        </div>    <div class="suggestionsBox" id="suggestions" style="display: none;">    <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />    <div class="suggestionList" id="autoSuggestionsList">       </div></div>  
      

  5.   

    其实你要的效果就是Google Suggest那样的效果:实现: 
    <script type="text/javascript" src="/path/to/SuggestFramework.js"></script> 
    <script type="text/javascript">window.onload = initializeSuggestFramework;</script> 
    有了上面两句后,每个取了名的文本框会多出五个属性: 
    1.action 必须。接受 GET 方式提交的数据,并返回相关 Javascript 数组的动态页。 
    2.capture 如果返回的结果不止一列(比如本例中的单词和中文意思),将要替换用户输入的那一列(从 1 开始算)。通常这个和数据库字段相对应。 
    可选,默认为 1. 
    3.columns 下拉显示的列数,比如本例中,按字母查询单词,并将中文意思显示在右侧。可选,默认为 1. 
    4.delay 查询延时,单位为毫秒。较低的延时会得到更快的反应,但会加重服务器负担。可选,默认为 1000(1秒)。 
    5.heading 如果设为 true ,第一个数组值将作为不可选择项(标题栏)。当有两列或两列以上数据时非常有用。可选,默认为 false. 
    数据提交只需要两个数据 
    1.type 输入框的name 
    2.q 搜索关键字(默认UTF-8编码) 
    您下载的压缩包中,已经包含php和ColdFusion示例,当然这个框架可以适用于所有的编程语言,无平台限制。后台数据输出就是一条 Javascript 语句。一维数组这么写: 
    new Array(”val1″, “val2″, “val3″); 
    二维数组这么写: 
    new Array( 
    new Array(”第1行条第1列”, “第1行第2列”), 
    new Array(”第2行条第1列”, “第1行条第2列”), 
    new Array(”第3行条第1列”, “第1行条第2列”) 
    ); 
    最后介绍一下,css中需要定义的4个类 
    .SuggestFramework_List 提示内容所在区域 
    .SuggestFramework_Heading 第一条提示 
    .SuggestFramework_Highlighted 设置高亮的一条提示 
    .SuggestFramework_Normal 其他提示 
    下载地址 http://xiazai.jb51.net/200903/yuanma/SuggestFramework-0.31.zip 
    详细出处参考:http://www.jb51.net/article/17442.htm
      

  6.   

    建议参考这篇文章:Exercise Explanations: Implementing AJAX Suggest and Autocomplete很容易上手的
      

  7.   

    在 rpc.php 的首行加上 header('Content-Type: text/html; charset=gb2312');