以下代码为html
代码:
<input type="search" name="srchtxt" id="srchtxt"/>
<div class="resultsearch">
<ul class="ul"></ul>
</div>
<a href="#" id="srchbtn"></a>
以下代码为jquery
代码:
$("#srchtxt").keyup(function(){
var addresshome = $("#srchtxt").val();
$.post("addressfile",{address:addresshome},function(res){
$(".ul").html(res);
});
});
$(".ul li#item a").each(function(){
$(this).click(function(e){
e.preventDefault();
var index_address,address;
index_address = $(this).parent().index();
alert(index_address);
address = $(this).eq(index_address).attr("value");
$("#srchtxt").attr("value",address);
});
});
$("#srchbtn").click(function(){
alert($("#srchtxt").val());
});
以下代码为php
代码:
$address = $_POST['address'];$querysrch = "select city,state,bolv from table where city like '%".$address."%' or state like '%".$address."%' or bolv like '%".$address."%'";$ressrch = mysqli_query($cnt,$querysrch);if(mysqli_num_rows($ressrch) > 0){
while($row = mysqli_fetch_assoc($ressrch)){
$addressres = $row['city']."-".$row['state']."-".$row['bolv'];
echo "<li id='item'><a href='#' value='".$addressres."'>".$addressres."</a></>";
}}else{ echo "<li id='item'><a href='#' value='not found'>notfound</a></li>";}
当结果显示时,我无法访问显示值的li
列表或插入到id =&#34; srchtxt&#34;
答案 0 :(得分:1)
试试这段代码:
$results = '';
$address = $_POST['address'];
$querysrch = "select city,state,bolv from table where city like '%".$address."%' or state like '%".$address."%' or bolv like '%".$address."%'";
$ressrch = mysqli_query($cnt,$querysrch);
if(mysqli_num_rows($ressrch) > 0){
while($row = mysqli_fetch_assoc($ressrch)){
$addressres = $row['city']."-".$row['state']."-".$row['bolv'];
$results .= "<li id='item'><a href='#' value='".$addressres."'>".$addressres."</a></li>";
}
}
else
{
$results .= "<li id='item'><a href='#' value='not found'>notfound</a></li>";
}
echo $results;
return;