gpt4 book ai didi

javascript - 仅当使用 jQuery 搜索框中的文本时才显示消息

转载 作者:行者123 更新时间:2023-11-28 10:17:50 24 4
gpt4 key购买 nike

我有一个用 jQuery 编写的 Google Instant 风格的搜索脚本,它查询 PHP 文件。目前,当未找到结果时,PHP 文件不会返回 HTML 代码,因此脚本会显示一条消息“未找到结果。”。但是,当搜索框中没有搜索词且查询字符串为空时,仍会显示此消息。我怎样才能得到它,以便仅当搜索框中有文本时才显示该消息?

这是我的 jQuery 代码:

$(document).ready(function(){
$("#search").keyup(function(){
var search=$(this).val();
var query=encodeURIComponent(search);
var yt_url='search.php?q='+query+'&category=web';
window.location.hash='search/'+query+'/1/';
document.title=$(this).val()+" - My Search Script";
if(search==''){
window.location.hash='';
document.title='My Search Script';
}
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(response){
if(response !=""){
$("#result").html(response);
} else {
$("#result").html("No results were found.");
}
}
});
});
});

最佳答案

即使搜索字段中没有任何内容,请求仍然会触发。仅当字段不为空时才发出请求。

关于javascript - 仅当使用 jQuery 搜索框中的文本时才显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6246131/

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