【发布时间】:2023-04-01 15:35:01
【问题描述】:
我有一个链接,我用 .preventDefault 方法取消了“点击”事件,以检查用户是否登录。如果用户使用正确的角色登录,我想继续点击链接后的点击效果,回家?
jQuery('.btn-read-more').on('click', function(e) {
e.preventDefault();
console.log(e.currentTarget);
var data = {
action: 'is_user_logged_in'
};
jQuery.post(ajaxurl, data, function(response) {
console.log(response);
if (response === 'no') {
jQuery('#modal_login_form_div').modal('show');
} else if (response === 'ccwdpo_user' || response === 'ccwdpo_customer') {
//e.currentTarget.click();
//console.log(e.currentTarget);
window.location = jQuery(this).attr("href");
}
});
});
现在,我解决了:
jQuery('.permalink').on('click', function(e) {
e.preventDefault();
var data = {
action: 'is_user_logged_in'
};
var permalink = jQuery(this).attr('href');
//console.log(permalink);
jQuery.post(ajaxurl, data, function(response) {
console.log(response);
if (response === 'no') {
jQuery('#modal_login_form_div').modal('show');
} else if (response === 'ccwdpo_user' || response === 'ccwdpo_customer') {
window.location = permalink;
} else {
jQuery('#modal_error_div').modal('show');
}
});
});
【问题讨论】:
-
this不是您认为的 post 回调中的内容。在帖子外存储对this或href的引用
标签: javascript jquery events onclick preventdefault