gpt4 book ai didi

php - 使用向上和向下键突出显示从 php ajax 搜索中获取的搜索文本

转载 作者:可可西里 更新时间:2023-10-31 23:30:10 26 4
gpt4 key购买 nike

我正在尝试切换从 ajax 结果集中获取的搜索结果。实际上,问题在于 onkeyup 和 onkeydown 它不会在搜索结果中上下移动,这非常令人沮丧。但是,我尝试的代码如下:-

代码:-

<input type="search" class="searchnews" name="news" autocomplete="off" />
<div id="suggestion-list"></div>
<style>
.selected {background: #EAEAEA;}
</style>
<script>
$('.searchnews').keyup(function(e){
$('input[name="hidden_news"]').val('');
$('input[name="hidden_news"]').val($.trim($(this).val()));
if(e.keyCode!=40 && e.keyCode!=38){
var str = $(this).val();
$.ajax({
type:'POST',
url:'ajax/getnews.php',
data:'str='+str,
success: function(data){
if(data.length <= 2){
$("#suggestion-list").css('display','none');
}
else{
$("#suggestion-list").html('');
$("#suggestion-list").css('display','block');
$("#suggestion-list").append(data);
}
}
});
}

});
$(document).keydown(function(e){
if (e.keyCode == 38) {
var selected = $(".selected");
$("#suggestion-list ul li").removeClass("selected");
if (selected.prev().length == 0) {
selected.siblings().last().addClass("selected");
} else {
selected.prev().addClass("selected");
}
}
if (e.keyCode == 40) {
var selected = $(".selected");
$("#suggestion-list ul li").removeClass("selected");
selected.next().addClass("selected");
if (selected.next().length == 0) {
selected.siblings().first().addClass("selected");
} else {
selected.next().addClass("selected");
}
}
});
</script>

**getnews.php**

<?php
if(isset($_POST['str']) && $_POST['str']!=''){
$str=$_POST['str'];
$str1 = explode(' ',$str);
foreach($str1 as $st){
$sql[] =" title like '%".$st."%'";
}
$result=mysql_query("SELECT * FROM news WHERE ".implode('and',$sql)." or title like '%".$str."%'");
//$result=mysql_query("SELECT * FROM newsupdate WHERE title LIKE \"%$str%\" and status='1' ORDER BY published_date DESC");
$i=0;
if(mysql_num_rows($result) > 0){
echo '<ul>';
while($row=mysql_fetch_array($result)){
$i++;
if($i==1){
?>
<li class="selected"><a style="color:#030303;" target="_blank" >
<span class="text"><?php echo preg_replace("/$str/i",'<span class="highlight">'.$str.'</span>',$row['title']); ?></span>
</a></li>
<?php
}
else{
?>
<li><a style="color:#030303;" target="_blank">
<span class="text"><?php echo preg_replace("/$str/i",'<span class="highlight">'.$str.'</span>',$row['title']); ?>
</span>
</a></li>
<?php
}
}
echo '</ul>';
}
}
?>

请帮我解决这个问题。谢谢。

最佳答案

Jason,能否请您删除我从您的代码中注释掉的行,并告诉我您这样做会发生什么?

$(document).keydown(function(e){
if (e.keyCode == 38) {
var selected = $(".selected");
$("#suggestion-list ul li").removeClass("selected");
if (selected.prev().length == 0) {
selected.siblings().last().addClass("selected");
} else {
selected.prev().addClass("selected");
}
}
if (e.keyCode == 40) {
var selected = $(".selected");
$("#suggestion-list ul li").removeClass("selected");
//selected.next().addClass("selected");
if (selected.next().length == 0) {
selected.siblings().first().addClass("selected");
} else {
selected.next().addClass("selected");
}
}
});

关于php - 使用向上和向下键突出显示从 php ajax 搜索中获取的搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315457/

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