我一直在我的PHP項目中混淆了一些JS和ajax,並且我遇到了一些問題。 ajax似乎只在點擊後刷新頁面或只是刷新點擊後才起作用。我使用jQuery 3.2.1。我是第一次使用.live()jQuery Ajax點擊不起作用,除非頁面被刷新
,但現在已被廢棄,我讀了。對()是一個另類,所以我使用了。
這是我的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);
}
}
});
}
});
});
有什麼我失蹤或者做錯了?
你好?您在Ajax'success'回調中使用['.load'](http://api.jquery.com/load/)?你驚訝地發現一些奇怪的行爲?而且你甚至不使用Ajax響應......這看起來很奇怪。 –
您沒有正確使用'on',如果您的ajax請求也替換了您點擊的元素,則需要使用**委託**版本 – adeneo
在js中使用php的地方使用雙引號 – C2486