gpt4 book ai didi

html - 使用Ajax但页面仍然刷新

转载 作者:行者123 更新时间:2023-12-01 07:28:44 26 4
gpt4 key购买 nike

我在搜索表单上有以下内容,但 Ajax 部分似乎仍在刷新整个页面。我的搜索结果返回得很好,但是,我希望它不要每次都刷新页面。

showLoader() 在刷新之前出现,但结果在刷新之后显示。

我的成功是否指向了错误的DIV?或者提交发生在错误的时间?我迷路了

    $('.em-events-search-form').submit(function(){
showLoader()
if( this.search.value=='<?php echo $s_default; ?>'){
this.search.value = '';
}
if( $('#em-wrapper .em-events-list').length == 1 ){
$(this).ajaxSubmit({
url : EM.ajaxurl,
data : {
_wpnonce : '<?php echo wp_create_nonce('search_states'); ?>',
return_html : true
},
success : function(responseText) {
$('#em-wrapper .em-events-list').replaceWith(responseText);
}
});
}
});

最佳答案

您需要阻止执行表单的默认操作:

$('.em-events-search-form').submit(function(e){
showLoader()
if( this.search.value=='<?php echo $s_default; ?>'){
this.search.value = '';
}
if( $('#em-wrapper .em-events-list').length == 1 ){
$(this).ajaxSubmit({
url : EM.ajaxurl,
data : {
_wpnonce : '<?php echo wp_create_nonce('search_states'); ?>',
return_html : true
},
success : function(responseText) {
$('#em-wrapper .em-events-list').replaceWith(responseText);
}
});
e.preventDefault();
}
});

两个细微的变化是,e(事件)参数现在被传递到函数中,一旦您设置了 AJAX 调用,您就可以阻止默认操作的执行。

关于html - 使用Ajax但页面仍然刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829184/

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