[英]Apply autocomplete to dynamically generated textboxes with data from database
I have gone through many ques here on stackoverflow with regard to my query here but could not apply them to my problem. 关于我的查询,我在stackoverflow上遇到了很多问题,但是无法将它们应用到我的问题中。
Hence I am giving here my files that I have been using. 因此,我在这里给出了我一直在使用的文件。
I am successfully able to use autocomplete with a static textbox. 我成功地将自动完成功能与静态文本框一起使用。 I get the data from the database.
我从数据库中获取数据。 But however I am not able to figure out how to do it for dynamically generated textboxes.
但是,我无法弄清楚如何为动态生成的文本框做到这一点。 I want autocomplete feature to be implemented for dynamically generated textboxes with data from databases
我希望为包含数据库数据的动态生成的文本框实现自动完成功能
here are my files: 这是我的文件:
index.php 的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 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";
}
}
?> ?>
Just call your .autocomplete()
function on your newly added inputs AFTER they have been added to the DOM 只需在新添加的输入添加到DOM后调用
.autocomplete()
函数
function addForm() {
$("#forms").append("<form><input type ='text' name='winner' id='winner'><br/></form>");
$("#winner").autocomplete("autocomplete.php", {
selectFirst: true
});
}
I suggest you put the init code ($('#id').autocomplete) for autocompletion on the js code part that update your page dynamically. 我建议你把init代码($('#id')。autocomplete)用于动态更新页面的js代码部分的自动完成。 I guess you're using ajax, so you should have someting like that
我猜你正在使用ajax,所以你应该有这样的东西
$.ajax({
// your ajax options here
}).done(
// here you update your html page
// and then set the autocomplete on the new element
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.