Javascript 使用AJAX删除前端的WordPress帖子
我正在尝试使用AJAX从前端删除WordPress帖子。 我的代码删除了帖子,但显示了带有“success”的空白页面,而我只想淡出这篇帖子,而不需要重新加载页面并显示空白页面 PHP代码:Javascript 使用AJAX删除前端的WordPress帖子,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在尝试使用AJAX从前端删除WordPress帖子。 我的代码删除了帖子,但显示了带有“success”的空白页面,而我只想淡出这篇帖子,而不需要重新加载页面并显示空白页面 PHP代码: <?php if( current_user_can( 'delete_post' ) ) : ?> <?php $nonce = wp_create_nonce('my_delete_post_nonce') ?> <a href="<?
<?php if( current_user_can( 'delete_post' ) ) : ?>
<?php $nonce = wp_create_nonce('my_delete_post_nonce') ?>
<a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<?php endif ?>
my_script.js代码:
jQuery( document ).ready( function($) {
$(document).on( 'click', '.delete-post', function() {
var id = $(this).data('id');
var nonce = $(this).data('nonce');
var post = $(this).parents('.post:first');
$.ajax({
type: 'post',
url: MyAjax2.ajaxurl,
data: {
action: 'my_delete_post',
nonce: nonce,
id: id
},
success: function( result ) {
if( result == 'success7' ) {
post.fadeOut( function(){
post.remove();
});
}
}
})
return false;
})
})
问题是页面正在重新加载到带有“success”文本的空白页面,这时它应该淡出并从当前页面删除帖子,而不需要重新加载
看起来my_script.js根本没有被使用:(
非常感谢您的帮助
<a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
成功7而不是成功不幸的是,点击链接或按钮后,使用#或制作按钮没有任何作用;/你知道为什么吗?它应该会起作用。看看这里:另外,你得到一个带有“成功”的“空”页面这一事实告诉我,你的ajax功能一般都在起作用。我想现在它只是一个打字错误:如果(result==“success7”)成功7而不是成功
<a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<a href=# data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<button data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</button>
if( result == 'success7' )