当我点击“点击此处添加更多单位”按钮时,它添加了更多我不想要的具有相同名称的字段。 我想要的是每次迭代时复制的输入都会得到新的名称,例如:
Unitmaster.1.name, Unitmaster.1.data1, Unitmaster.1.data2
Unitmaster.2.name, Unitmaster.2.data1, Unitmaster.2.data2
请帮帮我
$(document).ready(function() {
var unitcount = 1;
var topdiv = $('.alldata').html();
$('#addnewunit').click(function() {
//var namedata = topdiv.attr('name',"some-name"+unitcount);
//alert(namedata)
$('#addmoreunit').append('' + topdiv + '.<div class="col-md-3 removeid" style="float:right"><span class="btn btn-xs"><i class="icon-remove"></i> Remove</span></div></div>');
unitcount++;
var value = $('.target').val();
$('.list').val(value);
$('.removeid').click(function() {
$(this).parent('div').remove();
});
}); // JavaScript Document
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
<div class="alldata">
<select name="Unitmaster.0.name">
<option value="1">one</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
<input type="text" value="value1" name="Unitmaster.0.data1">
<input type="text" value="value1" name="Unitmaster.0.data2">
</div>
<div id="addmoreunit">
</div>
<div class="formSep" id="addnewunit" style="cursor:pointer">
<span class="btn btn-xs">
<i class="icon-plus">
</i>Click Here To Add More Unit
</span>
</div>
</body>