我有一些创建自动完成表单的 PHP 和 JQuery:
<?
mysql_select_db($database_database_connection, $database_connection);
$query = "SELECT * FROM Device_tbl";
$result=mysql_query($query, $database_connection) or die(mysql_error());
$findrow = array();
while($row = mysql_fetch_assoc($result)){
$manufac = $row['Manufacturer'];
$mod = $row['Model'];
$string = $manufac.' '.$mod;
$findrow[] = $string;
}
?>
<script type="text/javascript">
$(document).ready(function() {
$("#search_input").watermark("Begin Typing to Search");
var availableTags = <? echo json_encode($findrow);?>;
$( "#search_input" ).autocomplete({
source: availableTags
});
$("#search_input").bind("autocompleteselect", function() {
var search_input = $(this).val();
if (search_input =='') search_input ='*';
var dataString = 'keyword='+search_input;
if (search_input.length > 2 || search_input=='*') {
$.ajax({
type: "GET",
url: "core/functions/searchdata.php",
data: dataString,
success: function(server_response) {
$('#searchresultdata').empty();
$('#searchresultdata').append(server_response);
$('span#category_title').html(search_input);
}
});
}
return false;
});
$("#search_input").trigger('keyup');
});
</script>
搜索数据.php
mysql_select_db($database_database_connection, $database_connection);
$query = "SELECT Image, Manufacturer, Model FROM Device_tbl WHERE Manufacturer LIKE '%$keyword%' OR Model LIKE '%$keyword%'";
if ($keyword=='*') $query = "SELECT Image, Manufacturer, Model FROM Device_tbl";
$result=mysql_query($query, $database_connection) or die(mysql_error());
if($result){
if(mysql_affected_rows($database_connection)!=0){
while($row = mysql_fetch_object($result)){
?>
<div class="hold-cont">
<div class="holder">
<div class="image-hold" >
<img class="image-icon" src="<? echo $deviceimg.($row->Image); ?>"/>
</div>
</div>
<div class="device-name devicename-txt"><? echo($row->Manufacturer. ' ' .$row->Model); ?></div>
</div>
<?
}
}else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
}
else {
echo 'Parameter Missing';
自动完成部分工作正常,但是我无法工作的是在所选值上运行 .ajax。例如,如果我开始输入“Apple iMac”并且只输入“Ap”。建议将是 Apple iMac,但是当我单击建议时,它会在“Ap”上运行查询 - 即,它只运行关于输入内容的查询,而不是点击的内容。
当用户单击建议或点击输入时,我有什么想法可以使这项工作吗?