Jquery事件绑定新克隆的行

时间:2012-08-10 17:23:02

标签: jquery delegates clone

我一直在研究一个功能,我需要在表的每个单元格上实现自动保存,如果该单元格是最后一行的最后一个单元格,还要添加一个新行。新克隆的行也应该具有相同的绑定模糊事件。我能够添加新行,但事件没有被触发。这是代码.. 请帮忙坚持很久..

$("#myTable1").live("blur", function(){
         var elementValue = document.getElementById("<%=count%>").value.toString();
         var value = $("#<%=count%>_hidden").attr('value').toString();
         var qualValue = "<%=qualValue %>";
    var sequenceNumber = "<%=sequenceNumber %>";
    var parentAttrbtInstId = "<%=parentAttrbtInstId %>";
    var parentId = "<%=parentId %>";
    var attrbtId = "<%=attrbtId %>";
    var attrbtInstId =  $("#<%=count%>_hidden").attr('attrbtInstId').toString();
    var rootId = "<%=rootId %>";
    var itemId = "<%=item.getItemId()%>";
    var currentCount = "<%=count%>";
    var cellCount = "<%=cellLimit%>";
    var rowId = "row_<%=rowCounter%>";
    var rowCount = <%=rowCounter%>;



      if(elementValue.toLowerCase() != value.toLowerCase()){
         $.ajax({
           url: "<portlet:resourceURL id='autoSaveAction' />",
             type: 'POST',
             dataType: 'json',
             data: { elementValue:elementValue,qualValue:qualValue,sequenceNumber:sequenceNumber,parentAttrbtInstId:parentAttrbtInstId,parentId:parentId,attrbtId:attrbtId,
              attrbtInstId:attrbtInstId,rootId:rootId,itemId:itemId},
             success : function(response) {
                 if (response.status == "SUCCESS") {
                    jsonstr=JSON.stringify(response.result);
 var temp=JSON.parse(jsonstr);
 $("#<%=count%>_hidden").attr('attrbtInstId',temp.attrInstId);
 document.getElementById("<%=count%>_hidden").value = temp.attrValue;
 if(currentCount == cellCount){
 cloneRow = $("#myTable1 tr:last").clone(true);

     cloneRow.attr({
       'id': function(_, id) { 
 return "row_"+(rowCount+1);
       }            
     }).end().appendTo("#myTable1");

 cloneRow.find("input").each(function() {
     $(this).attr({
       'id': function(_, id) { 
 if (id.indexOf("hidden")!= -1){
 return currentCount + "_hidden";
 }else{
        return ++currentCount;
 }
       }             
     });
   }).end().appendTo("#myTable1");
 cloneRow.find("select").each(function() {
     $(this).attr({
       'id': function(_, id) {
       if (id.indexOf("hidden")!= -1){
 return currentCount + "_hidden";
 }else{
        return ++currentCount;
 }
 }              
     });
   }).end().appendTo("#myTable1");
 cloneRow.find("textarea").each(function() {
     $(this).attr({
       'id': function(_, id) {
        if (id.indexOf("hidden")!= -1){
 return currentCount + "_hidden";
 }else{
        return ++currentCount;
 } 
       }              
     });
   }).end().appendTo("#myTable1");
 }

 } else {
      }
      },
     error:function (xhr, ajaxOptions, thrownError){ 

     }
         });
          }
     });
         }); 

1 个答案:

答案 0 :(得分:0)

如果你希望表中的所有元素都有监听器,你首先需要给它们一个类,(比如“table-elem”),并附加一个这样的监听器:

   $("#myTable1").on("blur", ".table-elem", function() {

        //your function you want to run
   });