我有一张表格,并且在按下添加新链接后,我想克隆它的最后一行。它在我的行中只有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 + /);
我会建议不同的方法,因为每个问题的答案[这里](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) –