2017-05-05 38 views
5

我有一张表格,并且在按下添加新链接后,我想克隆它的最后一行。它在我的行中只有TextBox的情况下工作得很好,但当下拉时没有。请帮我修改jquery代码。 这里的表的代码:如何使用jQuery将下拉列表中的表格行复制到

<div><a href="#" id="addNew">Add New</a></div> 
       <table id="dataTable"> 
        <tr> 
         <th>Item</th> 
         <th>Cost</th> 
         <th></th> 
        </tr> 
        @if (Model != null && Model.Count > 0) 
        { 
         int j = 0; 
         foreach (var i in Model) 
         { 
          <tr> 
           <td>@Html.DropDownListFor(a => a[j].fk_purchase_id, (SelectList)ViewBag.fk_purchase_id, null, htmlAttributes: new { @class = "form-control"})</td> 
           <td>@Html.TextBoxFor(a => a[j].cost, htmlAttributes: new { @class = "form-control" })</td> 
           <td> 
            @if (j > 0) 
            { 
             <a href="#" class="remove">Remove</a> 
            } 
           </td> 
          </tr> 
          j++; 
         } 
        } 
       </table> 

这里是一个需要一些改进代码:

<script> 
     $(function() { 
      //1. Add new row 
      $("#addNew").click(function (e) { 
       e.preventDefault(); 
       var $tableBody = $("#dataTable"); 
       var $trLast = $tableBody.find("tr:last"); 
       var $trNew = $trLast.clone(); 
       alert($trNew.html); 
       var suffix = $trNew.find(':input:first').attr('name').match(/\d+/); 
       $trNew.find("td:last").html('<a href="#" class="remove">Remove</a>'); 
       $.each($trNew.find(':input'), function (i, val) { 
       // Replaced Name 
       var oldN = $(this).attr('name'); 
       var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']'); 
       $(this).attr('name', newN); 
       //Replaced value 
       var type = $(this).attr('type'); 
       if (type.toLowerCase() == "text") { 
       $(this).attr('value', ''); 
        } 
        }); 

       $trLast.after($trNew); 
      }); 

     }); 
    </script> 

我试图修改此行蒙山改变输入选择 ,但它没有帮助

var后缀= $ trNew.find(':input:first')。attr('name')。match(/ \ d + /);

+1

我会建议不同的方法,因为每个问题的答案[这里](HTTP:// stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)and [here](http://stackoverflow.com/questions/40539321/a -partial - 视图 - 传球 - 一个信息收集使用最HTML-begincollectionitem辅助/ 40541892#4054 1892) –

回答

2

首先在表中添加tbody像:

 <table id="dataTable"> 
     <thead> 
      <tr> 
       <th>Item</th> 
       <th>Cost</th> 
       <th></th> 
      </tr> 
      </thead> 
      <tbody> 
      @if (Model != null && Model.Count > 0) 
      { 
       int j = 0; 
       foreach (var i in Model) 
       { 
        <tr> 
         <td>@Html.DropDownListFor(a => a[j].fk_purchase_id, (SelectList)ViewBag.fk_purchase_id, null, htmlAttributes: new { @class = "form-control"})</td> 
         <td>@Html.TextBoxFor(a => a[j].cost, htmlAttributes: new { @class = "form-control" })</td> 
         <td> 
          @if (j > 0) 
          { 
           <a href="#" class="remove">Remove</a> 
          } 
         </td> 
        </tr> 
        j++; 
       } 
      } 
     </tbody> 
     </table> 

而且你的脚本是:

<script> 
    $(function() { 
     $("#addNew").click(function (e) { 
      e.preventDefault();      
      var last = $('#dataTable>tbody>tr:last'); 
      if (last.length > 0) { 
       var name = last.children().find('input,select')[0].name; 
       var index = Number(name.replace(/[^0-9]/gi, '')) + 1; 
       var tr = ('<tr>' + last.html().replace(/[0-9]+__/gi, index + '__') + '</tr>') .replace(/\[[0-9]+\]+[.]/gi, '[' + index + '].'); 
       $('#dataTable tbody').append(tr); 
      } 
     }); 

    }); 
</script> 
+0

感谢@Ashiquzzaman,它帮助复制,尽管现在将数据传递给控制器​​还有另一个问题。 **它只能通过最多2个模型项目,即使我有4行复制**以前只有文本框我可以发送所有的项目..它仍然与jQuery的东西,可能与索引... – Amelie

+0

你能请给我看看你的HTML。后4复制?@Amelie – Ashiquzzaman

+0

这里是我正在使用的全视图:https://jsfiddle.net/aiste/mp5cx5sL/ @Ashiquzzaman – Amelie

相关问题