我有一个带自动填充功能的文本框。 我想在文本框中选择值时显示一个div。我尝试了更改jquery ui的功能,但它无法正常工作。它仅在我们单击文本框时才起作用。我希望在文本框获得完整值时触发操作。
我在下面提供了我的代码:
的index.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:'search.php',
minLength:1,
change: function (event, ui) { alert("hi"); }
});
});
</script>
</head>
<body>
<form method="post" action="">
Name : <input type="text" id="name" name="name" />
</form>
</body>
</html>
的search.php
<?php
$con=mysqli_connect("localhost","root","","datadb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// ...some PHP code for database "my_db"...
// Change database to "test"
mysqli_select_db($con,"datadb");
// ...some PHP code for database "test"...
$term=$_GET["term"];
$query=mysqli_query($con,"SELECT * FROM user where FirstName like '%".$term."%' GROUP by FirstName ");
$json=array();
if (!$query) { // add this check.
die('Invalid query: ' . mysql_error());
}
while($student=mysqli_fetch_array($query)){
$json[]=array(
'value'=> $student["FirstName"],
'label'=>$student["FirstName"]
);
}
echo json_encode($json);
?>
答案 0 :(得分:0)
要检测您在自动填充中选择其中一个选项,可以使用autocompleteselect
事件......
$("#name").autocomplete({
source: 'search.php',
minLength: 1
}).on('autocompleteselect',function() {
// Do your stuff here
alert("hi");
});
我希望它有所帮助