我设计了一个搜索表单。我想使用ajax,php和mysql选择目的地。当我使用ajax从数据库中获取的列表从第一个文本框中选择值时,会发生什么?该选项也被选为另一个文本框。
这是我的ajax代码:
$(document).ready(function(){
$('#location').keyup(function(){
var query = $(this).val();
if(query != '')
{
$.ajax({
url:"search.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#countryList').fadeIn();
$('#countryList').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#location').val($(this).text());
$('#countryList').fadeOut();
});
ajax for another destination input:
$('#des').keyup(function(){
var query = $(this).val();
if(query != '')
{
$.ajax({
url:"search2.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#destinationList').fadeIn();
$('#destinationList').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#des').val($(this).text());
$('#destinationList').fadeOut();
});
//Form:
<form>
<br>
<div class="row">
<div class="form-group col-sm-5">
<label for="usr">From</label>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
<input id="location" type="text" class="form-control" name="loc" placeholder="Location">
</div>
<div id="countryList"></div>
</div>
<div class="col-sm-2">
</div>
<div class="form-group col-sm-5">
<label for="usr">To</label>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
<input id="des" type="text" class="form-control" name="des" placeholder="Destination">
</div>
<div id="destinationList"></div>
</div>
</div>