我试图在另一个函数中调用ajax函数但是因为我已经创建了一个循环,所以相同的值会再次传递,并且会再次循环调用 让我告诉你我的代码
$(window).load(function(e) {
// grab the scroll amount and the window height
loadmore();
select_likes();
select_share();
// get_recieve_friend_requests();
// get_sent_friend_requests();
});
function loadmore() {
var lastID = $('.load-more').attr('lastID');
// alert(lastID);
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url("user/get_all_post"); ?>',
data: {
id: lastID
},
dataType: 'json',
beforeSend: function(data) {
$('.load-more').show();
},
success: function(data) {
var ParsedObject = JSON.stringify(data);
var json = $.parseJSON(ParsedObject);
if (json == "") {
$("#bottom").append('<div class="btn btn-default col-md-6" >' + 'No More Results' + '</div>');
$("#Load_more_data").hide();
} else {
$postID = json[json.length - 1].id;
$('.load-more').attr('lastID', $postID);
$.each(json, function(key, data) {
var post_id = data.id;
var data_id = [data.length - 1].id;
// alert(data_id);
var post_status = data.status;
var status_image = data.status_image;
var multimage = data.multimage;
var commentID = $('.comment-more').attr('commentID');
// alert(comment_post_id);
if (!post_status == "" && !status_image == "") {
$("#status_data").append('<div class="col-md-6 postdata' + post_id + '"><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">' + post_status + '</div><div class="weshare_user_singleimage"><img style="height:300px; width:400px;" src="<?php echo base_url('
uploads '); ?>/' + status_image + '"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a onclick="like(' + post_id + ');"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a onclick="select_comment(' + post_id + ');" ><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a onclick="share(' + post_id + ');"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li><button type="button" id="comment_show" onclick="select_comment(' + post_id + ');"><i class="fa fa-caret-down "></i></button></ul></div></div><div class="panel-footer " id="comment_div" onload="select_comment(' + post_id + ');"><div class="row"><div class="col-md-12"><a href="#">13 people</a> like this</div></div><ul class="media-list"><li class="media_comment"></li><li class="media"><div class="media-left media-top"><?php echo img($user_file_image); ?></div><div class="media-body"><div class="input-group"><form action="" id="form_content_image"><textarea name="textdata" id="content_comment_image" cols="25" rows="1" class="form-control message" placeholder="Whats on your mind ?"></textarea><button type="submit" id="comment_button_image" onclick="comment_here_image(' + post_id + ');">Comment</button></form></div></div></li></ul></div></div>');
} else if (!post_status == "" && status_image == "") {
$("#status_data").append('<div class="col-md-6 postdata' + post_id + '" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">' + post_status + '</div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a onclick="like(' + post_id + ');"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a onclick="select_comment(' + post_id + ');"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a onclick="share(' + post_id + ');"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li><button type="button" id="comment_show" onclick="select_comment(' + post_id + ');" ><i class="fa fa-caret-down "></i></button></ul></div></div><div class="panel-footer " id="comment_div" onload="select_comment(' + post_id + ');"><div class="row"><div class="col-md-12"><a href="#">13 people</a> like this</div></div><ul class="media-list"><li class="media_comment"></li><li class="media"><div class="media-left media-top"><?php echo img($user_file_image); ?></div><div class="media-body"><div class="input-group"><form action="" id="form_content"><textarea name="textdata" id="content_comment" cols="25" rows="1" class="form-control message" placeholder="Whats on your mind ?"></textarea><button type="button" id="comment_button" onclick="comment_here(' + post_id + ');" >Comment</button><?php echo form_close();?></div></div></li></ul></div></div>');
} else if (!multimage == "") {
$("#status_data").append('<div class="col-md-6 postdata' + post_id + '" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname; ?></a><div class="weshare_user_multimage"><img style="height:300px; width:400px;" src="<?php echo base_url('
uploads '); ?>/' + multimage + '"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a onclick="like(' + post_id + ');"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a onclick="select_comment(' + post_id + ');"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a onclick="share(' + post_id + ');"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li><button type="button" id="comment_show" onclick="select_comment(' + post_id + ');"><i class="fa fa-caret-down "></i></button></ul></div></div><div class="panel-footer " id="comment_div" onload="select_comment(' + post_id + ');"><div class="row"><div class="col-md-12"><a href="#">13 people</a> like this</div></div><ul class="media-list"><li class="media_comment' + post_id + '"></li><li class="media"><div class="media-left media-top"><?php echo img($user_file_image); ?></div><div class="media-body"><div class="input-group"><form action="" id="form_content_multimage"><textarea name="textdata" id="content_comment_multimage" cols="25" rows="1" class="form-control message" placeholder="Whats on your mind ?"></textarea><button type="submit" id="comment_button_multimage" onclick="comment_here_multimage(' + post_id + ');" >Comment</button><?php echo form_close();?></div></div></li></ul></div></div>');
} else {
}
});
});
}
});
}
function select_comment(post_id) {
// alert(post_id);
var Post_id = post_id;
var User_id = $('.id_data').attr('value');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url("user/select_comment"); ?>',
data: {
Post_id: Post_id,
User_id: User_id
},
dataType: 'json',
success: function(data) {
var ParsedObject = JSON.stringify(data);
var json = $.parseJSON(ParsedObject);
$.each(json, function(key, data) {
var comment = data.comment;
var post_id = data.post_id;
$post_id = post_id;
// alert(comment);
// alert(post_id);
$("#comment_post_id").attr('value', $post_id);
$(".media_comment").append('<li class=" media-top"><?php echo img($user_file_image); ?> <p>' + comment + '</p> <br><a href="#">Like</a> · <a href="#">Reply</a> </li>');
});
}
});
}
}
function comment_here($post_id) {
$(document).on('click', '#comment_button', function(e) {
// this will prevent form and reload page on submit.
e.preventDefault();
// here you will get Post ID
var Post_id = $post_id;
var User_id = $('.id_data').attr('value');
var textdata = $('#content_comment').val();
// alert(textdata);
$.ajax({
type: 'POST',
url: '<?php echo base_url("user/post_comment"); ?>',
data: {
Post_id: Post_id,
User_id: User_id,
textdata: textdata
},
dataType: 'json',
success: function(data) {
console.log(data);
alert('you have like this');
var ParsedObject = JSON.stringify(data);
var json = $.parseJSON(ParsedObject);
$postID = json[json.length - 1].post_id;
alert($postID);
$('.comment-more').attr('commentID', $postID);
jQuery('#form_content')[0].reset();
}
});
});
}
function comment_here_image($post_id) {
$(document).on('click', '#comment_button_image', function(e) {
// this will prevent form and reload page on submit.
e.preventDefault();
// here you will get Post ID
var Post_id = $post_id;
var User_id = $('.id_data').attr('value');
var textdata = $('#content_comment_image').val();
alert(textdata);
$.ajax({
type: 'POST',
url: '<?php echo base_url("user/post_comment"); ?>',
data: {
Post_id: Post_id,
User_id: User_id,
textdata: textdata
},
dataType: 'json',
success: function(data) {
console.log(data);
alert('you have like this');
jQuery('#form_content_image')[0].reset();
Post_id = "";
}
});
});
}
function comment_here_multimage($post_id) {
$(document).on('click', '#comment_button_multimage', function(e) {
// this will prevent form and reload page on submit.
e.preventDefault();
// here you will get Post ID
var Post_id = $post_id;
var User_id = $('.id_data').attr('value');
var textdata = $('#content_comment_multimage').val();
// alert(textdata);
alert(Post_id);
$.ajax({
type: 'POST',
url: '<?php echo base_url("user/post_comment"); ?>',
data: {
Post_id: Post_id,
User_id: User_id,
textdata: textdata
},
dataType: 'json',
success: function(data) {
console.log(data);
alert('you have like this');
jQuery('#form_content_multimage')[0].reset();
Post_id = "";
}
});
});
}
&#13;
现在我在这段代码中做的是首先在页面上加载一个函数调用loadmore();这个loadmore包含我的数据库中的所有帖子,通过使用json [json.length-1]降低id来显示在我的页面上。现在我想评论那些帖子所以我在我的代码中附加了textarea但是当我单击comment_here(post_id)的注释按钮正确传递的post_id但是每当我选择一个帖子并对其进行评论时,首先调用循环中的id然后点击&#39;点击&#39; id调用所以我需要停止循环并在注释中删除id以及我做错了什么?如果你有什么不明白的地方告诉我。