gpt4 book ai didi

php - 使用 Ajax 的 HTML 搜索建议不会使用键盘自动完成

转载 作者:行者123 更新时间:2023-11-28 03:45:01 25 4
gpt4 key购买 nike

我正在使用 MySql 数据库用 PHP 开发一个 Web 应用程序,并使用 ajax 制作了一个搜索建议框。问题是我可以用鼠标选择建议,它会自动完成,但是当我尝试用键盘自动完成它时(使用向上箭头选择它并按下回车键)它什么都不做。这是我的 css、ajax 还是 html 的问题???你能帮忙解决这个问题吗?感谢所有帮助,谢谢。

这是我的代码: 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

   .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

$(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

<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>

最佳答案

请引用这个Autocomplete library docs .

您可以实现普通的 AJAX 来获取记录并使用自动完成库呈现这些记录。

关于php - 使用 Ajax 的 HTML 搜索建议不会使用键盘自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com