我想为两个输入字段提供自动建议。对于第一个字段自动建议工作正常..但在第二个字段中自动建议不起作用。我已经检查了两次,但找不到我做了什么错误。
$(document).each(function(){
$('#user-searchbus').keyup(function(){
var val = $(this).val();
$.ajax({
method:'post',
url:'../php/ajax.php',
data:{key:val}
}).done(function( data ) {
if(data == ''){
$("#results").hide().html('');
}else{
$("#resultqueries").show().html(data);
$("#resultqueries div").each(function () {
$(this).click(function () {
var text = $(this).text();
$("#userqueries").val(text);
$("#resultqueries").hide().html('');
});
});
}
});
});
});
//ajax search
$(document).ready(function(){
$('#userqueries').keyup(function(){
if(val !=''){
$('#resultqueries').show.html('');
var cities = [
'Mumbai',
'Delhi',
'Bengaluru',
'chennai',
'kolkata',
'Hyderabad',
'Madurai',
'Ahmedabadh',
'kanpur',
'nagpur',
'jaipur',
'Indore',
'Thane',
'Bhopal',
'Coimbatore',
'vijayawada',
'rajkot',
'Allahabad',
'Salem',
'Thiruchi',
'Nellore',
'Dhindukal',
'Sikar',
'Raichpur',
'Haridwar',
'kasi',
'Thirunelveli'
];
$.each(cities,function(index,value){
if(value.toLowerCase().indexOf(val) >= 0){
$('#resultqueries').append("<div>"+value+"</div>");
}
});
$('#resultqueries div').each(function(){
$(this).click(function(){
var text = $(this).text();
$('#cities').val(text);
$('#resultqueries').hide().html('');
});
});
}else{
$('#resultqueries').hide.html('');
}
});
$(".scroll").click(function(){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top},1200);
});
});
我的PHP代码是
<?php
$con=mysqli_connect('localhost','root','madhi1234','busticketreservation')orDie();
$key = $_POST['key'];
if($key == ''){
echo "";
}else{
$ajaxsuggestionquery =
"SELECT cities FROM cities WHERE cities LIKE'%".$key."%'";
$ajaxresult = mysqli_query($con, $ajaxsuggestionquery);
while ( $row = mysqli_fetch_array($ajaxresult)) {
echo "<div>".$row['cities']."</div>";
}
}
&GT;