2017-05-05 24 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

相關問題