2015-11-02 79 views
0

我希望当我点击+添加更多按钮时,它应该添加与第1行相同的元素,并且S.No应该增加......并且每行都有删除选项。在表中添加<tr>元素

code in JSFIDDLE

<a href="" id="c" class="text-right">+add more</a> 

<table class="table"> 
    <thead> 
     <tr> 
      <th>S.No.</th> 
      <th>Name</th> 
      <th>Dose</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>1</td> 
      <td style="width:80%"> 
       <input id="1a" type="text" class="form-control"> 
      </td> 
      <td style="width:80%"> 
       <input type="number" class="form-control"> 
      </td> 
     <tr> 
    </tbody> 
</table> 
+1

的[添加表行jQuery中]可能的复制( HTTP:// stackoverfl ow.com/questions/171027/add-table-row-in-jquery) – 0X0nosugar

回答

2

您可以使用.clone()复制你的第一tr

$(".text-right").on("click", function() { 
 
    var tr = $("table tr:eq(1)").clone(true); 
 
    $(tr).find("td:first").text($("table tr").length - 1); 
 
    $("table").append(tr); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="#" id="c" class="text-right">+add more</a> 
 
<table class="table"> 
 
    <thead> 
 
    <tr> 
 
     <th>S.No.</th> 
 
     <th>Name</th> 
 
     <th>Dose</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>1</td> 
 
     <td style="width:80%"> 
 
     <input type="text" class="form-control"> 
 
     </td> 
 
     <td style="width:80%"> 
 
     <input type="number" class="form-control"> 
 
     </td> 
 
     <tr> 
 
    </tbody> 
 

 
</table>

我从投入事业id删除id必须是唯一的。还要修改一下代码,以增加td的数值正确。

+0

但是S.No.不增加 –

+0

@ SaurabhDellaRa'Man检查更新答案。 –

2

jquery clone()是用于克隆DOM元素,然后你可以附加它。考虑代码是这样的:

$(function(){ 
    $("#c").click(function(e){ 
     e.preventDefault(); 
     $("table.table tbody tr:first").clone().appendTo("table.table tbody"); 
    }); 
}) 

DEMO


对于序列号的增量:

$(function(){ 
    var counter = 1; 
    $("#c").click(function(e){ 
     e.preventDefault(); 
     var tr = $("table.table tbody tr:first").clone().appendTo("table.table tbody"); 
     tr.find("td:eq(0)").text(++counter); 
    }); 
}) 

See it in action

+0

但S.No不增加 –

+0

更新回答@ SaurabhDellaRa'Man序列号增量看看 – Manwal

+0

完美...你是天才......但我可以删除选项也删除特定行 –

0

尝试: HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <a href="#" id="c" class="text-right">+add more</a> 
    <table class="table"> 
     <thead> 
     <tr> 
      <th>S.No.</th> 
      <th>Name</th> 
      <th>Dose</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td>1</td> 
      <td style="width:80%"> 
      <input type="text" class="form-control"> 
      </td> 
      <td style="width:80%"> 
      <input type="number" class="form-control"/></br><a class="rm" href="#">remove</a> 
      </td> 
      </tr> 
     </tbody> 

    </table> 

JS:

function updateNumber() { 
    x = 0 
     $('table.table tbody tr').each(function(i,v){ 

      $(this).find('td:eq(0)').text(x+1) 
      x++; 
     }); 
} 
    $("#c").on('click',function(e){ 
     e.preventDefault(); 
     var tr = $("table.table tbody tr:first").clone().appendTo("table.table tbody"); 
     updateNumber(); 
     console.log($('table.table tbody tr')); 
    }); 
    $('body').on("click",".rm", function(e) { 
     e.preventDefault(); 
     $(this).parents('tr').remove(); 
     updateNumber(); 
    }); 

的jsfiddle:https://jsfiddle.net/pjeruccb/3/

0

看一看这样的:

$(".text-right").on("click", function() { 
 
    var prevNo = parseInt($(".table tr:last td:first-child").text()); 
 
    var tr = $("table tr:eq(1)").clone(true); 
 
    $("table").append(tr); 
 
    $('.table tr:last-child td:first-child').html(prevNo + 1); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="#" id="c" class="text-right">+add more</a> 
 
<table class="table"> 
 
    <thead> 
 
    <tr> 
 
     <th>S.No.</th> 
 
     <th>Name</th> 
 
     <th>Dose</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>1</td> 
 
     <td style="width:80%"> 
 
     <input type="text" class="form-control"> 
 
     </td> 
 
     <td style="width:80%"> 
 
     <input type="number" class="form-control"> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 

 
</table>