我一直在我的 PHP 项目中使用一些 JS 和 ajax,我遇到了一些问题。 ajax 似乎仅在单击后刷新页面或仅刷新以单击时才起作用。我正在使用 jquery 3.2.1。
我最初使用的是 .live(),但现在已弃用,我读到 .on() 是替代方案,所以我现在正在使用它。
这是我的 JS 部分:
$(".up_vote").on("click", function() {
var post_id = $(this).attr('postid');
$.ajax({
type: "POST",
data: {
'post_id': post_id,
'action': 'upvote'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
} else {
alert(response.Message);
}
}
});
});
$(".down_vote").on("click", function() {
var post_id = $(this).attr('postid');
$.ajax({
type: "POST",
data: {
'post_id': post_id,
'action': 'downvote'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
} else {
alert(response.Message);
}
}
});
});
$("#btnpost").click(function() {
var post = $("#post_feed").val();
if (post == "") {
alert("This can't be empty.");
return false;
} else {
$.ajax({
type: "POST",
data: {
'post_feed': post,
'action': 'post'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$("#post_feed").val("");
$('#feed_div').load('<?php echo $url; ?>/index.php #feed_div');
} else {
alert(response.Message);
}
}
});
}
});
});
我是否遗漏或做错了什么?
最佳答案
首先确保您击中了目标,试试这个,看看警报是否显示:
$(function() {
$(document).on('click', '.up_vote', function() {
alert('ok');
});
});
点击事件附加到页面加载时的现有元素。这里的关键词是“现有”。当我们用 ajax 加载一些东西时,我们操纵了 DOM。我们正在放置全新的元素。所以我们需要做的是在放置新内容后附加该事件。
所以为了在每次调用 ajax 时附加事件,我们使用 .on
相应地编辑你的代码
$(document).on('click', '.up_vote', function() {
var post_id = $(this).attr('postid');
$.ajax({
type: "POST",
data: {
'post_id': post_id,
'action': 'upvote'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
} else {
alert(response.Message);
}
}
});
});
$(document).on('click', '.down_vote', function() {
var post_id = $(this).attr('postid');
$.ajax({
type: "POST",
data: {
'post_id': post_id,
'action': 'downvote'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
} else {
alert(response.Message);
}
}
});
});
$(document).on('click', '#btnpost', function() {
var post = $("#post_feed").val();
if (post == "") {
alert("This can't be empty.");
return false;
} else {
$.ajax({
type: "POST",
data: {
'post_feed': post,
'action': 'post'
},
url: '<?php echo $url; ?>/includes/functions.php',
dataType: 'json',
success: function(response) {
if (response.ResponseCode == 200) {
$("#post_feed").val("");
$('#feed_div').load('<?php echo $url; ?>/index.php #feed_div');
} else {
alert(response.Message);
}
}
});
}
});
});
关于javascript - 除非刷新页面,否则 jQuery Ajax 点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169394/