How can I clone a row in a table without cloning the values of the input elements inside it?

后端 未结 3 1770
猫巷女王i
猫巷女王i 2021-01-13 10:46

I am trying to add a row to a table. I found that we can use the clone() method to duplicate an existing row. My table has two text inputs in it in two different <

相关标签:
3条回答
  • 2021-01-13 11:14

    The Given Above answer is Perfect but One thing is there missing the created rows dint not have the events that are aquired by previous created rows. so to clone identical rows with events we have to set property true .

    see here......

      var newRow = $("#table-of-items     tr:last").clone(true).find(':input').val('').end();
    
      $("#table-of-items").append(newRow);
    

    Note table-of-items is the name of table.

    0 讨论(0)
  • 2021-01-13 11:15

    You can add this line after your given line to set the values of the input fields to blank:

    $('#table-1 tr:last input').attr('value','');
    

    or just:

    $('#table-1 tr:last input').val('');
    
    0 讨论(0)
  • 2021-01-13 11:30

    Try this:

    var clone = $("#table-1 tr:last").clone().find('input').val('').end();
    
    • .clone() the last <tr>
    • .find() the <input> elements in the clone
    • set the .val() of the <input> elements to '',
    • call .end() so that the cloned <tr> is stored in the variable instead of the <input> elements.

    If you intend to append it to the table immediately, add .insertAfter("#table-1 tr:last") to the end.

    var clone = $("#table-1 tr:last").clone().find('input').val('').end().insertAfter("#table-1 tr:last");
    
    0 讨论(0)
提交回复
热议问题