嗨,我有一个ajax搜索框,它从我的post类型中提取我所有的分类法,但是如果我开始键入并且分类法不存在,它只显示一个小框,里面什么都没有。如果分类法不存在,它会弹出框中说‘找不到搜索结果’吗?
这是我的职责:
function fetch(){
var keyword = jQuery('#listing_tags').val();
var myDiv = document.getElementById("ajaxbox");
if (keyword != "")
{
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action','keyword': keyword},
function(response){
jQuery('#datafetch').html('');
jQuery('#datafetch').append(response);
});
}
jQuery(document).on('click','#datafetch li',function(){
var val = jQuery(this).html();
jQuery('#listing_tags').val(val);
jQuery('#datafetch').html('');
var myDiv = document.getElementById("ajaxbox");
myDiv.style.display = "none";
});发布于 2020-05-20 12:17:32
您可以随时检查响应。我不知道php函数返回什么,但假设它在未找到的情况下什么也不返回。让我们修改fetch()函数来检查响应是否为空。从下面的代码中,您将知道在哪里实现弹出窗口。
function fetch() {
var keyword = jQuery('#listing_tags').val();
var myDiv = document.getElementById("ajaxbox");
if (keyword != "") {
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
jQuery.post('<?php echo admin_url('
admin - ajax.php '); ?>', {
'action': 'my_action',
'keyword': keyword
},
function(response) {
jQuery('#datafetch').html('');
if(response.trim()==''){
//This code will run when response is empty
jQuery('#datafetch').append('<p>no search results found</p>');
}else{
//This code will run if there is valid response
jQuery('#datafetch').append(response);
}
});
}https://wordpress.stackexchange.com/questions/367105
复制相似问题