2015-11-05 60 views
1

以下代碼可以編輯row.but當我嘗試編輯另一行時,第一次單擊的行仍處於可編輯狀態,以禁用該行嗎?根據用戶選擇編輯行

$("#tableid").on("click", "tr", function() { 
     $(this).find('td').each(function() { 
      var matter = $(this).text(); 
      if (matter != "") { 
       $(this).parents('tr').css('background-color', 'red'); 
       $(this).addClass("editclass"); 
       $(this).html("<input type='text' value='" + matter + "'/>"); 
       $(this).children().first().focus(); 

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

一個的jsfiddle會幫助別人幫你 – smerny

回答

1

當點擊TR只是執行刪除類在TR

喜歡這個

$("#tableid td").each(function() { 
    if ($(this).hasClass("editclass")) { 
     $(this).parents("tr").css("background-color", "white"); 
     $(this).html($(this).find("input").val()); 
     $(this).removeClass("editclass"); 
    } 
}) 

JSFIDDLE

+0

從我可以告訴,'TR '這個行中'editclass' – smerny

+0

''(this).removeClass(「editclass」);'這將是 –

+0

該行正在刪除t他從'td'類 – smerny