Add/delete row from a table

前端 未结 7 1538
盖世英雄少女心
盖世英雄少女心 2020-12-01 04:51

I have this table with some dependents information and there is a add and delete button for each row to add/delete additional dependents. When I click \"add\" button, a new

相关标签:
7条回答
  • 2020-12-01 05:19

    Hi I would do something like this:

    var id = 4; // inital number of rows plus one
    function addRow(){
       // add a new tr with id 
       // increment id;
    }
    
    function deleteRow(id){
       $("#" + id).remove();
    }
    

    and i would have a table like this:

    <table id = 'dsTable' >
          <tr id=1>
             <td> Relationship Type </td>
             <td> Date of Birth </td>
             <td> Gender </td>
          </tr>
          <tr id=2>
             <td> Spouse </td>
             <td> 1980-22-03 </td>
             <td> female </td>
             <td> <input type="button" id ="addDep" value="Add" onclick = "add()" </td>
             <td> <input type="button" id ="deleteDep" value="Delete" onclick = "deleteRow(2)"  </td>
          </tr>
           <tr id=3>
             <td> Child </td>
             <td> 2008-23-06 </td>
             <td> female </td>
             <td> <input type="button" id ="addDep" value="Add" onclick = "add()"</td>
             <td>  <input type="button" id ="deleteDep" value="Delete" onclick = "deleteRow(3)" </td>
          </tr>
       </table>
    

    Also if you want you can make a loop to build up the table. So it will be easy to build the table. The same you can do with edit:)

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