2 liwei3773 liwei3773 于 2014.07.25 10:00 提问

jquery的click事件多次绑定

.tools-comment是评论按钮的class名,点击它弹出回复评论层。添加回复内容,点发表,发表成功,将返回的数据写入页面中。此时不刷新页面,再次点击评论,发表,则一下发表了两条相同的数据,ajax请求了两次。第三次则发表三条......第N次发表N条。.editWindow-send就是弹出层上面的发表按钮的class名,代码如下,请大神帮看看是什么原因。
var topicid,artsid,articleid;
$("#found").on("click",".tools-comment",function (){
//跳转评论
var tempHtml='';
var url="index.php?app=weixin&ac=wnote&ts=verifyUser";
$.ajax({
url:url,
type:"get",
async:false,
datatype:"json",
success:function(data)
{
var data=$.parseJSON(data);
if(data.code==1)
showEditWindow();
if(data.code==0)
popWindowAlert(data.msg);
if(data.code==4)
location.href=data.ts_url;
if(data.code==5)
location.href=data.ts_url;
}
});

        //这里需要记录当前点击是哪个话题
         articleid=$(this).children(".docomment").attr("articleid");
         artsid=$(this).children(".docomment").attr("artsid");
                     topicid=$(this).children(".docomment").attr("topicid");
                     if(articleid){
            var url="index.php?app=weixin&ac=wnote&ts=addArticleComment";
            $(".editWindow-send").on("click",function (){
                                        var content=$("#ComentContent").val();
                                        $.ajax({
                                        url:url,
                                        type:"post",
                                        datatype:"json",
                                        async:false,
                                        data:{"articleid":articleid,"content":content},
                                        success:function(data){
                                                var data=$.parseJSON(data);
                                                //执行登陆跳转
                                                if (data.status==800) 
                                                {
                                                        popWindowAlert(data.msg);
                                                }
                                                //执行成功返回跳转
                                                if(data.status==1){
                                                        var htmlStr="<li class='reply-again' id="+data['res']['commentid']+" comment_id="+data['res']['commentid']+"><div class='ku_replyp delcomment' id="+data['res']['commentid']+" to_userid="+data['res']['commentid']+" comment_id="+data['res']['commentid']+"><div class='reply-photo'><a href='index.php?app=weixin&ac=wcircleZone&userid="+data['res']['userid']+"' class='ui-link'><img src='"+data['res']['face']+"'></a></div><div class='ku_replyp_text'>"+data['res']['username']+":</div> <div class='ku_replyp_con'>"+data['res']['content']+"</div></div><div class='reply-time'><div class='reply-time-l'>"+data['res']['addtime']+"</div> <div class='reply-time-r'> <ul><li class='glm-love'><i></i><span class='likenum'></span></li> <li class='glm-line'>|</li> <li class='comment-reply' to_comment_id='300' comment_id='300'><i></i></li> </ul></div> </div> <div class='reply-box-all'>  </div><div class='clear'></div> <div class='br10'></div></li>";
                                                        $(".gl-replay-list").append(htmlStr);
                                                }else{
                                                //执行失败
                                                        popWindowAlert(data.msg);
                                                }
                                        }
                                });
                            });
        }
    });

1个回答

qwuack
qwuack   2014.07.25 17:23

把.editWindow-send的onClick放到外面,跟#found同一个等级

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!