1
我正在构建一个AJAX实时搜索,它的工作原理,但我想使用键盘箭头(上/下)导航。我不知道我该怎么做。添加箭头导航到Ajax实时搜索
我form.php的
<div class="input-group" id="nav-input-group" style="display:table;">
<input name="q" id="thesearchbar" class="form-control input-search " name="search" placeholder="Serach..." autocomplete="off" type="text" onclick="">
<div class="result"></div>
</div>
我的脚本
$(document).ready(function(){
$('#nav-input-group 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("_ajax_search.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("#nav-input-group").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
和_ajax_search.php
<?php
require('bdd_pdo_connect.php');
try{
if(isset($_REQUEST['term'])){
$sql = "SELECT * FROM subcategories WHERE subcategory LIKE :term";
$stmt = $bdd->prepare($sql);
$term = '%' . $_REQUEST['term'] . '%';
$stmt->bindParam(':term', $term);
$stmt->execute();
if($stmt->rowCount() > 0){
while($row = $stmt->fetch()){
echo "<p>" . $row['subcategory'] . "</p>";
}
} else{
echo "<p>No matches found";
}
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
unset($bdd);
?>
我是新的AJAX和任何帮助将不胜感激。
我看不到你的代码中的任何AJAX调用... – Hulothe
哪里是在你的代码Ajax调用? –
对不起,我只是更新我的文章 – Rubyx