2015-11-08 68 views
0

我有以下代碼,我可以在行中編輯上下文,並且每行都包含一個保存按鈕。當我編輯並保存上下文時,它工作正常,但是當我嘗試編輯並不保存它並嘗試編輯其他行時,我編輯的上下文無需單擊保存按鈕即可保存。我該如何恢復原始值?將編輯後的數據保存在html行中

$("#tableid").on("click", "tr", function() 
{ 
    $(this).find(".btnsave").attr("disabled", false); 
    $(".btnsave").click(function() { 
     $("#tableidtd").each(function() 
     { 
      if ($(this).hasClass("editclass")) 
      { 
       $(this).parents('tr').css("background-color", "white"); 
       $(this).html($(this).find('input').val()); 
       $(this).removeClass("editclass"); 
       $(".btnsave").attr("disabled", true); 
      } 
     }); 
    }); 
    $("#tableid td").each(function() { 
     if ($(this).hasClass("editclass")) { 
      $(this).parents('tr').css("background-color", "white"); 
      $(this).html($(this).find('input').val()); 
      $(this).removeClass("editclass"); 
     } 
    }); 
    $(this).find('td:not(:first-child, :last-child)').each(function() 
    { 
     var oldcontent= $(this).text(); 
     if (oldcontent!= " ") 
     { 
      $(this).parents('tr').css('background-color', 'red'); 
      $(this).addClass("editclass"); 
      $(this).html("<input type='text' value='" + oldcontent+ "'/>"); 
      $(this).children().first().focus(); 
      $(this).children().first().keypress(function (e) 
      { 
       if (e.which == 13) 
       { 
        $(this).parents('tr').css('background-color', 'white'); 
        var newtext= $(this).val(); 
        $(this).parent().text(newtext); 
        $(this).removeClass("editclass"); 
       } 
      }); 
     } 
    }); 
    $(this).children().first().blur(function() { 
     $(this).parent().text(oldcontent); 
     $(this).removeClass("editclass"); 
    }); 
    return false; 
}) 
+0

你可以提供一個[的jsfiddle(http://jsFiddle.net) – Taleeb

+0

@Taleeb HTTP:/ /jsfiddle.net/tom_boy/zqc5Le4o/這裏是我的小提琴,但它不wrking.may你會得到一個主意。 – learner

回答

0

從我從你的問題理解 - 你需要保持它可以用來在有需要時恢復值的行的(tr)數據的備份。在下面的例子中,我使用的陣列來存儲備份

arrBackupData = []; 
$("#tableid").on("click", "td", function() { 
    if ($(this).find('.btnsave').length > 0) { 
     return; 
    } 
    var tRow = $(this).closest('tr'); 
    $(tRow).find(".btnsave").attr("disabled", false); 

    $("#tableid td.editclass").each(function() { 
     $(this).parents('tr').css("background-color", "white"); 
     if (arrBackupData.length > 0) { 
      $(this).html(arrBackupData.shift()); 
     } else { 
      $(this).html($(this).find("input").val()); 
     } 
     $(this).removeClass("editclass"); 
    }); 
    arrBackupData = []; 

    $(tRow).find('td:not(:first-child, :last-child)').each(function() { 
     var oldcontent = $(this).text(); 
     if (oldcontent != " ") { 
      arrBackupData.push(oldcontent); 
      $(this).parents('tr').css('background-color', 'red'); 
      $(this).addClass("editclass"); 
      $(this).html("<input type='text' value='" + oldcontent + "'/>"); 
      $(this).children().first().focus(); 

      $(this).children().first().keypress(function (e) { 
       if (e.which == 13) { 
        $(this).parents('tr').css('background-color', 'white'); 
        var newtext = $(this).val(); 
        $(this).parent().text(newtext); 
        $(this).removeClass("editclass"); 
       } 
      }); 
     } 
    }); 
    $(tRow).children().first().blur(function() { 
     $(this).parent().text(oldcontent); 
     $(this).removeClass("editclass"); 
    }); 
    return false; 
}); 

$(".btnsave").click(function() { 

    $("#tableid td.editclass").each(function() { 

     $(this).parents('tr').css("background-color", "white"); 
     $(this).html($(this).find('input').val()); 
     $(this).removeClass("editclass"); 
     $(".btnsave").attr("disabled", true); 

    }); 
}); 

查看更新後的jsfiddle here

+0

我的問題是,如果我編輯的值,並單擊保存按鈕它應該被保存..如果我編輯第一個TD並轉到另一行,第一個編輯的行應該返回舊值 – learner

+0

@learner。我已經更新了答案。看到更新的jsFiddle [這裏](http://jsfiddle.net/taleebanwar/zqc5Le4o/2/) – Taleeb

+0

謝謝你,其實上一個也在工作。+ 1給你。謝謝男人:) @Taleeb – learner

相關問題