0

我一直在研究一个功能,我需要在表格的每个单元格上实现自动保存,并且如果该单元格是最后一行的最后一个单元格,则添加一个新行。新克隆的行也应该具有相同的绑定模糊事件。我可以添加新行,但没有触发事件。这是代码..请帮助卡住很长时间..

$("#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){ 

     }
         });
          }
     });
         }); 
4

1 回答 1

0

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

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

        //your function you want to run
   });
于 2012-08-10T23:41:13.817 回答