在我的.js文件的以下代码中,第一个函数save_audition运行得很好。第二个,我只是复制/粘贴并更改了一些变量,由于某种原因,它不是。我做了一些故障排除,排除了我的.php文件(手动调用时它工作得很好)和.html,并确定这个脚本由于某种原因无法触发。
这是我第一个使用AJAX的项目,所以它可以是非常简单的东西,不确定。仅供参考,我正在使用admin-ajax.php在Wordpress的AJAX框架中工作
编辑:这是主插件文件的粘贴箱:http://pastebin.com/AaJ6QqTx
以下是.js文件中的代码。第一个(‘click’)事件(save_audition)工作正常,第二个事件(hide_audition)工作在return false;阻止链接触发的意义上,但实际的AJAX单击事件并没有发生。
//Save (or remove) an audition from the user's saved auditions meta using ajax
$('.save-audition').on('click', function() {
var post_id = $(this).attr('data-post_id');
var nonce = $(this).attr('data-nonce');
var clicked = $(this);
$.ajax({
type : 'post',
dataType : 'json',
url : myAjax.ajaxurl, //we can use this value because in our php file we used wp_localize_script
data : {action: 'tps_save_audition', post_id : post_id, nonce: nonce},
success: function(response) {
$('#alerts').append('<div class="alert"><div class="alert-icon"><i class="fa fa-exclamation"></i></div><div class="alert-message">'+response.message+'</div><div class="close"><i class="fa fa-close"></i></div></div>');
$('#alerts .alert').fadeIn(400);
if(response.type == 'success') {
clicked.children('span').children('.fa-stack-2x').addClass('fa-check');
clicked.children('span').children('.fa-stack-1x').addClass('saved');
} else if (response.type = 'removed') {
clicked.children('span').children('.fa-stack-2x').removeClass('fa-check');
clicked.children('span').children('.fa-stack-1x').removeClass('saved');
} else {
alert(response.message);
}
}
})
return false;
});
//Hide an audition from the user's view by saving it as user_meta
$('.hide-audition').on('click', function() {
var post_id = $(this).attr('data-post_id');
var nonce = $(this).attr('data-nonce');
var clicked = $(this);
$.ajax({
type : 'post',
dataType : 'json',
url : myAjax.ajaxurl, //we can use this value because in our php file we used wp_localize_script
data : {action: 'tps_hide_audition', post_id : post_id, nonce: nonce},
success: function(response) {
$('#alerts').append('<div class="alert"><div class="alert-icon"><i class="fa fa-exclamation"></i></div><div class="alert-message">'+response.message+'</div><div class="close"><i class="fa fa-close"></i></div></div>');
$('#alerts .alert').fadeIn(400);
if(response.type == 'success') {
clicked.children('i').css('color', 'red');
} else {
alert(response.message);
}
}
})
return false;
});发布于 2016-05-04 00:47:17
好的,像往常一样,它归结为一个愚蠢的编码器错误。在我的html中,有两个元素不小心包含了类"hide-audition“,这就破坏了它。
我想我会把这个问题留给其他人,以提醒他们在进行故障排除时仔细梳理每段代码。
https://stackoverflow.com/questions/37008310
复制相似问题