РЕДАКТИРОВАТЬ: Вот код JavaScript, я думаю, что вам нужно. Это предполагает, что "/ Rages / NewRateDetail" возвращает другую копию <table class="prettyForm">
. Смотрите полный автономный пример (в простом HTML) ниже.
$(".addDetail").click(function() {
if ($("#rateFormContainer").css("display")!= "none") {
$.ajax({
url: "/Rates/NewRateDetail/",
cache: false,
success: function(html) {
$("#rdContainer").append(html);
$("#rdContainer table.prettyForm").each(function(i, el) {
$("input,select,textarea", el).each(function(j,formEl) {
$(formEl)
.attr("name", $(formEl).attr("name").replace(/\[\d\]/, "["+ i +"]"))
.attr("id", $(formEl).attr("id").replace(/\[\d\]/, "["+ i +"]"));
});
});
}
});
$("#rateDetailMsg").empty()
}
});
Вот код, который копирует всю таблицу prettyForm каждый раз, когда вы нажимаете кнопку «Добавить». Затем он просматривает каждую таблицу и обновляет атрибуты поля id
и name
, соответствующие текущему индексу цикла.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#add").click(function(e) {
e.preventDefault();
$("table.prettyForm:first").clone().insertBefore(this);
$("table.prettyForm").each(function(i, el) {
$("input,select,textarea", el).each(function(j,formEl) {
$(formEl)
.attr("name", $(formEl).attr("name").replace(/\[\d\]/, "["+ i +"]"))
.attr("id", $(formEl).attr("id").replace(/\[\d\]/, "["+ i +"]"));
});
});
return false;
})
});
</script>
</head>
<body>
<table class="prettyForm">
<thead>
<th colspan="2">
Add Rate Details
<input type="hidden" value="0" name="RateDetail.Index" id="RateDetail_Index">
</th>
</thead>
<tr>
<td>Effective Date</td>
<td><input type="text" value="" name="RateDetail[0].EffectiveDate" id="RateDetail[0]_EffectiveDate"></td>
</tr>
<tr>
<td>Expiration Date</td>
<td><input type="text" value="" name="RateDetail[0].ExpirationDate" id="RateDetail[0]_ExpirationDate"></td>
</tr>
<tr>
<td>Status</td>
<td><select name="RateDetail[0].Status" id="RateDetail[0].Status"></select></td>
</tr>
</table>
<button id="add">Add Form</button>
</body>
</html>