When I enter some value in form field I get ajax suggestions. But when I click on some value from suggestions, I can't set it in form field.
<form name="form_search" method="GET" action="" id='form-for-search'>
<input type='hidden' name='search_type' value='simple'>
<input type='text' name='q' size='30' class='search' id='searchid'>
<br />
<div id='result'></div>
<br>
<input type='submit' value='Search'> <br>
</form>
This is my javascript/jquery code. I was trying with changing ".live()" function with ".on()" but it didn't help.
In genully this part should work $('#searchid').val($decoded);
But the $decoded var is always empty.
$(".search").keyup(function() {
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!=''){
$.ajax({
type: "POST",
url: "ajax/po_search.php",
data: {"search": searchid},
cache: false,
success: function(html){$("#result").html(html).show();}
});
}
return false;
});
jQuery("#result").on("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('name');
var $decoded = $("<div/>").html($name).text();
$('#searchid').val($decoded);
jQuery(document).live("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search")){
jQuery("#result").fadeOut();
}
});
$('#searchid').click(function(){
jQuery("#result").fadeIn();
});
});
jQuery:
$("#searchid").autocomplete({source: "ajax/po_search.php",minLength: 1});
po_search.php code:
$q=$_GET['term'];
$query="SELECT DISTINCT NAME FROM tblpartners partners WHERE NAME LIKE '%".$q."%'";
$result =mysqli_query($connection,$query);
while($row=mysqli_fetch_array($result)){
$username=$row['NAME'];
$arr[]=$username;
}
echo json_encode($arr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.