簡體   English   中英

如何基於1個輸入進行自動完成並顯示2-3個其他輸入的信息

[英]How make an autocomplete based on 1 input and show information for 2-3 other intput

我想創建一個像這樣的自動完成功能: http : //www.codemashups.com/source/jquery/jquery-autocomplete-p1/ 如果需要,可以嘗試使用代碼:1000、1001。
我已經完成了自動完成。 例如,如果我輸入Pa則表示Paris
我需要幫助的地方是:如果我感謝輸入1中的建議,單擊了巴黎,那么我想在輸入2中填寫“國家”:假設France 並在輸入3個Europe 這是我的文件autocomplete.php中的代碼:

<html>
   <head>
        <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
        <link rel="stylesheet" type="text/css"
        href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

        <script type="text/javascript">
                $(document).ready(function(){
                    $("#name").autocomplete({
                        source:'getautocomplete.php',
                        minLength:1

                    });
                });
        </script>




        </script>
   </head>

   <body>

      <form method="post" action="">
            <input type="text" id="name" name="name" />
      </form>

      <form method="post" action="">
            <input type="text" id="client" name="client" />
      </form>

   </body>
<html>

這是我的代碼Getautocomplete.php

//skip the code to connect for my DB

$con = mysql_connect($host_name,$user_name,$pass_word) or die(mysql_error());
mysql_select_db($database_name, $con) or die(mysql_error());



 $term=$_GET["term"];
 $query=mysql_query("select * from projets where proNum like '%".$term."%' order by proNum");
 $json=array();

    while($student=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $student["proNum"],
                    'label'=>$student["proNum"]
                        );
    }

 echo json_encode($json);

任何想法 ? 感謝您的幫助。

我已經遇到了這個問題,我的解決方案是,我向自動完成源添加了更多屬性,在您的情況下,您必須添加更多諸如state或php中的屬性,如下所示:

while($student=mysql_fetch_array($query)){
     $json[]=array(
                'value'=> $student["proNum"],
                'label'=>$student["proNum"],
                'state'=>$student["state"],
                'any_param'=>$student["any_param"]
                    );
}

在javascript自動完成選擇事件上,您可以獲取以下屬性:

$("#tags").autocomplete({
source: dataSearch,
select: function(event, ui) {
   alert(ui.item.state);//will alert you the state
       console.log(ui.item); //to view all properties of the selected item 
}
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM