javascript - When clicking the "Reply" button, will multiple pieces of data be inserted when clicking "Reply" for the second time?
过去多啦不再A梦
过去多啦不再A梦 2017-05-19 10:41:22
0
2
481

Online function demonstration address (mobile phone): http://bbs2.valueonline.cn/we...
Full project code download: http://pan.baidu.com/s/1bpDznhP

When the following button is clicked to reply for the second time, multiple pieces of data will be inserted. Can anyone please tell me why?

Main js code

<script type="text/javascript">
     var onOff=true;
//收缩二级评论
$(".btnReportNum").click(function(){
    var id=$(this).attr("data-id");
    var id2="reportBox"+id;
    if(onOff){
        $("#id2").hide();
        onOff=false;
    }
    else{
        $("#id2").show();
        onOff=true;
    }
})
//文字长度限制,收缩
var showMore='<a href="javascript:" class="activeColor btnSS" data-onOff="true">展开说明</a>';
$(".detailTxt").each(function(){
    var startTxt=$(this).html();
    var length=$(this).html().length;
    var maxNum=76;
    if(length>maxNum){
        //过滤空格
        var newT=$.trim(startTxt).substring(0,maxNum)+"...";
        $(this).html(newT);
        $(this).after(showMore);
        //点击收缩内容;
        $(this).next().click(function(){
            var onOff=$(this).attr("data-onOff");
            if(onOff=="true"){
                $(this).prev().html(startTxt);
                $(this).html("收起");
                $(this).attr("data-onOff","false");
            }
            else{
                $(this).prev().html(newT);
                $(this).html("展开说明");
                $(this).attr("data-onOff","true");
            }
        })
    }
})

//切换回复面板;
$(".btnCannel").click(function(){
    $(".page2").hide();
    $(".page").show();
})

//点击后要获取-
//主贴(一级)的ID(post_id),跟贴(二级)的ID(id),评论帖子(三级)的ID,回复用户的user_id,回复的内容(content)
//评论答案
var userUrl='information.html';  //登陆用户的个人主页链接
var imgAva="images/img.jpg";  //登陆用户的头像
var userName="tuihou";  //登陆用户的用户名
var floorName=10;  //一共有多个条跟帖

function showEdit(){
    $(".page2").show();
    $(".page").hide();
}

//评论一级回复
$(".btnReply1").click(function(){
    showEdit();
    $("textarea.anwerArea").focus();
    $(".btnSubmit").click(function(){
         var replyContent=$("textarea.anwerArea").val();
         var replyNum=parseInt($("#replyNum").html());
         var reply1='<p class="answerOne"> <p class="userRow"> <a href="'+userUrl+'"> <img src="'+imgAva+'" alt="" class="imgAva">'+userName+'</a> </p> <p class="pb15"> <span class="detailTxt">'+replyContent+'</span> </p> <p class="answerSmall"> <span class="mr10">'+(floorName+1)+'楼</span>刚刚 <a href="javascript:" class="btnReply2 btnReply" data-id="0">回复</a> </p> </p>';
         $("#content").append(reply1);
         $(".page2").hide();
         $(".page").show();
    })
})


//判断是否要加包裹层;
 var box='<p class="reportBox"> <img src="images/arrowsTop.png" alt="" class="arrowsTop"> <ul class="listReply2"> </ul> </p>';
//回复二级评论
$(".btnReply2").click(function(){
    showEdit();
    var temp="temp"+$(this).attr("data-id");
    $(this).addClass(temp);
    var temp='.'+temp;
    $(".btnSubmit").click(function(){
        var onOff=$(temp).parent().next().is(".reportBox");
        if(onOff){
        }
        else{
            $(temp).parent().after(box);
        }
        //获取要加的内容到ul中;
        var replyContent=$("textarea.anwerArea").val();
        var reply2='<li> <h4> <a href="'+userUrl+'">'+userName+'</a></h4><p class="reportTxt2">'+replyContent+'<span class="time">刚刚</span> <a class="time btnReply3" href="javascript:">回复</a> </p> </li>';
        $(".page2").hide();
        $(".page").show();
        $(temp).parent().next().find(".listReply2").append(reply2);
    })
})

</script>
过去多啦不再A梦
过去多啦不再A梦

reply all(2)
黄舟

Unbind after running

//Comment level reply
$(".btnReply1").click(function(){

showEdit();
$("textarea.anwerArea").focus();
$(".btnSubmit").click(function(){
     var replyContent=$("textarea.anwerArea").val();
     alert(replyContent);
     var replyNum=parseInt($("#replyNum").html());
     var reply1='<p class="answerOne"> <p class="userRow"> <a href="'+userUrl+'"> <img src="'+imgAva+'" alt="" class="imgAva">'+userName+'</a> </p> <p class="pb15"> <span class="detailTxt">'+replyContent+'</span> </p> <p class="answerSmall"> <span class="mr10">'+(floorName+1)+'楼</span>刚刚 <a href="javascript:" class="btnReply2 btnReply" data-id="0">回复</a> </p> </p>';
     $("#content").append(reply1);
     $(".page2").hide();
     $(".page").show();
     $(".btnSubmit").unbind();//这里要解绑,否则会在第二次执行两次的
})

})

小葫芦

I didn’t read it carefully, I suggest you register $(".btnSubmit").click(function(){}放到外边来,否则你每次点击.btnReply1都会为.btnSubmita click callback first...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template