0

ここでのクエリに関して、stackoverflow で多くのクエリを実行しましたが、それらを私の問題に適用できませんでした。

したがって、私が使用しているファイルをここに示します。

静的テキストボックスでオートコンプリートを正常に使用できます。データベースからデータを取得します。しかし、動的に生成されたテキストボックスに対してそれを行う方法を理解することはできません。データベースからのデータを使用して動的に生成されたテキストボックスにオートコンプリート機能を実装したい

ここに私のファイルがあります:

index.php

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Auto Complete Input box</title>
   <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
  <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript" src="jquery.autocomplete.js"></script>
    <script>
     $(document).ready(function(){
    $("#name").autocomplete("autocomplete.php", {
    selectFirst: true
});
 });
 </script>

  <script>
   function addForm(){
  $("#forms").append(
    "<form><input type ='text' name='winner' id='winner'><br/></form>"
   );
 }
  </script>
 </head>

  <body>
  <label>Name:</label>
   <input name="name" type="text" id="name" size="20"/>
   <input type="button" name="addmore" id="addmore" value="Add More Winners" onclick="addForm();"/>
<div id = "dyanamic"></div>
 </body>
 </html>

autocomplete.php

  <?php
require 'config.php';
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
//$mysqli=mysql_connect('localhost','root','','emp_db') or die("Database Error");
$sql="SELECT name FROM employee WHERE name LIKE '%$my_data%' ORDER BY name";
$result = mysql_query($sql) or die(mysqli_error());

if($result)
{
    while($row=mysql_fetch_array($result))
    {
        echo $row['name']."\n";
    }
}

?>

4

2 に答える 2

0

ページを動的に更新する js コード部分にオートコンプリート用の初期化コード ($('#id').autocomplete) を配置することをお勧めします。私はあなたがajaxを使用していると思うので、そのようなものが必要です

$.ajax({
    // your ajax options here
}).done(
    // here you update your html page
    // and then set the autocomplete on the new element
)
于 2013-09-26T15:36:10.603 に答える