我有一个页面,其中所有帖子都由php显示,而循环像这样 - >
Post 1
here is post 1 comment form
Post 2
here is comment form 2
这是html部分
php while loop starts here
<div id="tickers"> </div>
<form action="#" class="form-horizontal inner-all" id="show<?php echo $id; ?>">
<input name="mesgid" type="hidden" id="mesgid" value="<?php echo $id ?>">
<input name="usernames" id="usernames" type="hidden" value="<?php echo $usermsg ?>">
<input class="form-control" type="text" name="com" id="com" placeholder="Respond with a comment...">
<button type="submit" class="btn btn-theme tyiop" id="data<?php echo $id;?>"></button>
</form>
php while loop ends here
这里是jquery部分
<script>
$(function() {
$(".tyiop").click(function()
{
var mesgid = $("#mesgid").val();
var usernames = $("#usernames").val();
var comment = $("#com").val();
var dataString = 'mesgid='+ mesgid + '&usernames=' + usernames + '&comment=' + comment;
if(mesgid=='' || usernames=='' || comment=='')
{
alert('Please Give Valid Details');
}
else
{
$(".flasher").show();
$(".flasher").fadeIn(400).html('Loading Comment...');
$.ajax({
type: "POST",
url: "ajax/commentor.php",
data: dataString,
cache: false,
success: function(html){
$("#tickers").append(html);
$("#flasher").hide();
}
});
}return false;
}); });
</script>
每当我点击帖子评论时,评论都会附加到其他帖子div,如何才能将数据仅附加到我发布的帖子上
答案 0 :(得分:0)
像这样使用,在这里我采用点击元素的参考:
<script>
$(function() {
$(".tyiop").click(function()
{
var container = this;
var mesgid = $("#mesgid").val();
var usernames = $("#usernames").val();
var comment = $("#com").val();
var dataString = 'mesgid='+ mesgid + '&usernames=' + usernames + '&comment=' + comment;
if(mesgid=='' || usernames=='' || comment=='')
{
alert('Please Give Valid Details');
}
else
{
$(".flasher").show();
$(".flasher").fadeIn(400).html('Loading Comment...');
$.ajax({
type: "POST",
url: "ajax/commentor.php",
data: dataString,
cache: false,
success: function(html){
$(container).parent('form').prev("#tickers").append(html);
$("#flasher").hide();
}
});
}return false;
}); });
</script>