jquery使用live绑定toggle事件,第一次点击没有反应,第2次才起作用问题

今天做个功能,从后台取数据,有操作列,操作列有编辑和删除,点击编辑按钮变为保存,表格变为可编辑。自己拼的table,在实现时需要用到toggle事件,代码为:

$("#popChapterManage .JS-edit").live("click",function(){
        $(this).toggle(function () {
            $(this).text("保存");
            var input = $(this).parent().siblings(".input");
            var inputtext = input.text();
            input.html("<input id='txteditchapterName' class='ipt-text grid-12' type='text' value=''>");
            input.find("input").val(inputtext);
        },
           function () {
               var chapterId = $(this).attr("aId");
               if ($("#txteditchapterName").val() == "") {
                   alert("章节名称不能为空");
                   return;
               }
               $.ajax({
                   url: "/Teacher/Resource/AddandUpdateChapter",
                   type: "POST",
                   async: true,
                   data: {
                       Id: chapterId,
                       ChapterName: $("#txteditchapterName").val(),
                       ChapterSource: EnumList.CaseSource.Custom,

                   },
                   success: function (data) {
                       alert(data);
                       GetTheoryChaptertable();
                   }
               });


               $(this).text("编辑");
               var inputtext = $(this).parent().siblings(".input").find("input").val();
               $(this).parent().siblings(".input").html(inputtext);
           });

        //先触发click,不然第一次失效
        $(this).trigger("click");
    });

开始没有写

 $(this).trigger("click");这句,造成每次触发都要点击2次才能实现。

posted on 2015-08-04 16:13  魂游天外  阅读(617)  评论(0)    收藏  举报

导航