[英]HTML Search suggestion with Ajax doesn't auto complete with keyboard
I am developing a web application in PHP with a MySql database, and made a search suggestion box with ajax. 我正在使用MySql数据库在PHP中开发Web应用程序,并使用ajax创建了搜索建议框。 The problem is that I can select the suggestions with my mouse and it auto completes but when I try to auto complete it with my keyboard (select it with arrow up arrow down and pressing enter) it does nothing. 问题是我可以用鼠标选择建议,并且建议会自动完成,但是当我尝试使用键盘自动完成建议(使用向上的向下箭头并按下Enter进行选择)时,它什么也没做。 Is this a problem of my css, ajax or just html??? 这是我的CSS,ajax还是HTML的问题??? Could you please help with this, All help is appreciated, Thanks. 您能帮忙吗,感谢所有帮助,谢谢。
Here is my code : Ajax 这是我的代码:Ajax
$term = mysqli_real_escape_string($link, $_REQUEST['term']); if(isset($term)){
// Attempt select query execution
$sql = "SELECT * FROM something WHERE name LIKE '" . $term . "%' LIMIT 4";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<p>" . $row['name'] . "</p>";
}
// Close result set
mysqli_free_result($result);
} else{
echo "<p>No Results</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
} }
CSS CSS
.result{ position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 0px solid #CCCCCC;
border-top: none;
cursor: pointer;
background: #ffffff;
}
.result p:hover{
background: #f2f2f2;
}
JAVASCRIPT JAVASCRIPT
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("something.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
}); });
HTML HTML
<form align=center method='POST' action='index.php?cmd=list-something'>
<h1 class="text-center">Search</h1>
<div class="form-group">
<div class="input-group search-box">
<input class="form-control" type="text" autocomplete="off" name="search" id="search" placeholder="Search"/>
<div class="result"></div>
<span class="input-group-btn">
<button class="btn btn-success" type="submit"><span class="glyphicon glyphicon-search" aria-hidden="true"><span style="margin-left:10px;">Pesquisar</span></button>
</span>
</span>
</div>
</div>
</form>
Please refer this Autocomplete library docs . 请参考此自动完成程序库文档 。
You can implement normal AJAX for fetching records and render those records using Autocomplete library. 您可以实现普通的AJAX来获取记录并使用自动完成库呈现这些记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.