duanguoping2016 2016-05-08 07:22
浏览 291
已采纳

具有相同类名的不同元素的相同jquery

I have got this HTML that i get from PHP while loop

<div class="WriteCom <?php echo $Class?>">
  <form class="c_comment " method="post"><input type="text" class="Comment_In" ></form></div>
</div>  

And this Jquery

var commentcount=0;
$(".WriteCom").children().submit(function() {
    var numItems = $(this).parent().siblings('.F_W_comments').children('.CommentsAw').length;
    var cc=$(this).children().val();
    var i=$(this).parent().siblings(".c_jq").children().attr("data-i");
    var com=$(this);
    commentcount++;
    $(this).children().val('');
       $.ajax({
            type: "GET",
            url: '../connect.php',
            data: "C=" + cc+"&I="+i,
            success:function(data) {
                var json_x = $.parseJSON(data);
                com.parent().parent().siblings('.c_jq').children(".Comments").children(".L_CS").html(json_x[0]);
                if (numItems>=4) {
                    com.parent().siblings('.F_W_comments').children('.CommentsAw:first').remove();
                }
                com.parent().siblings('.F_W_comments').append('<div class="cn_'+commentcount+' CommentsAw Comment_HsJ"><img src="../users/'+json_x[3]+'"><span>'+json_x[2]+'</span><span class="s2">'+json_x[1]+'</span></div>');  
                if (commentcount==1) {
                    $(".Comment_Hs:first").css("marginTop","-75px");
                    $(".cn_1").css("marginBottom","15px");
                    }else if (commentcount==2) {
                        $(".Comment_Hs:first").css("marginTop","-90px");
                        $(".cn_1").css("marginBottom","0px");
                        $(".cn_2").css("marginBottom","15px");
                        $(".cn_2").css("marginTop","-10px");
                    }else if (commentcount==3) {
                        $(".Comment_Hs:first").css("marginTop","-105px");
                        $(".cn_3").css("marginTop","-10px");
                        $(".cn_3").css("marginBottom","15px");
                        $(".cn_2").css("marginBottom","0px");
                    }else if (commentcount==4) {
                        $(".Comment_HsJ:first").css("marginTop","-105px");
                        $(".cn_4").css("marginTop","-10px");
                        $(".cn_4").css("marginBottom","15px");
                        $(".cn_3").css("marginBottom","0px");
                    }else{
                        $(".Comment_HsJ:first").css("marginTop","-105px");
                        $(".Comment_HsJ:eq(3)").css("marginTop","-10px");
                        $(".Comment_HsJ:eq(3)").css("marginBottom","15px");
                        $(".Comment_HsJ:eq(2)").css("marginBottom","0px");
                }
            }         
        });                
       return false;    
});

So when i have several posts and i comment on first post commentcount becomes 1 and when i comment on second post commentcount becomes 2.But i want to have commentcount for each uploaded file individually.

  • 写回答

1条回答 默认 最新

  • douying6206 2016-05-08 07:37
    关注

    Put the comment count in the DIV for the post.

    $(".WriteCom").children().submit(function() {
        var commentcount = $(this).parent().data("commentcount") || 0;
        commentcount++;
        $(this).parent().data("commentcount", commentcount);
        // rest of code
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题