Making row editable when hit row edit button

后端 未结 3 570
暗喜
暗喜 2020-12-01 20:19

I\'m new to jQuery and JavaScript. I\'m trying to click on my Edit button in my table and make the entire row editable. For some reason it\'s not working. I think it\'s o

相关标签:
3条回答
  • 2020-12-01 20:39

    Try this:

    $('.editbtn').click(function() {
        var $this = $(this);
        var tds = $this.closest('tr').find('td').filter(function() {
            return $(this).find('.editbtn').length === 0;
        });
        if ($this.html() === 'Edit') {
            $this.html('Save');
            tds.prop('contenteditable', true);
        } else {
            $this.html('Edit');
            tds.prop('contenteditable', false);
        }
    });
    

    jsFiddle

    0 讨论(0)
  • 2020-12-01 20:46

    Try this. I created right now. I hope this can help.

    jQuery(document).ready(function() {
    
      $('#edit').click(function () {
    
          var currentTD = $(this).closest('tr');
    
          if ($(this).html() == 'Edit') {                  
    
             $(currentTD).find('.inputDisabled').prop("disabled",false);   
    
          } else {
    
             $(currentTD).find('.inputDisabled').prop("disabled",true);   
    
          }
    
          $(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
    
      });
    

    });

    https://jsfiddle.net/mkqLdo34/1/

    0 讨论(0)
  • 2020-12-01 20:52

    Your code with the button click is too complicated. I have reduced it by making it easier to understand.

      $(document).ready(function () {
          $('.editbtn').click(function () {
              var currentTD = $(this).parents('tr').find('td');
              if ($(this).html() == 'Edit') {                  
                  $.each(currentTD, function () {
                      $(this).prop('contenteditable', true)
                  });
              } else {
                 $.each(currentTD, function () {
                      $(this).prop('contenteditable', false)
                  });
              }
    
              $(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
    
          });
    
      });
    

    Code Explained:

    1) Get all the tds within tr using below code

    var currentTD = $(this).parents('tr').find('td');   
    

    2) Then as usual iterate through each tds and change its contenteditable property like below

    $.each(currentTD, function () {
                          $(this).prop('contenteditable', true)
                      });
    

    Updated JSFiddle

    0 讨论(0)
提交回复
热议问题