我有一个表单,我希望能够根据需要多次复制一组字段。而且我还想让这些新字段组的 field id
、name
和 label 的for
属性增加 1。到目前为止,我已经用 jQuery 尝试过这个,并让它至少复制了这组字段,但是 remove 不起作用. 而且我不确定如何为这三个属性中的每一个做 +1。我很感激我能得到的任何帮助。
这是它的一个jsfiddle, http://jsfiddle.net/Unfxn/
HTML
<form method="post" action="#" class="inlineForm" enctype="multipart/form-data">
<div class="repeatingSection">
<a href="#" class="buttonGray buttonRight deleteFight">Delete</a>
<input type="hidden" name="fighter_a_id_1" id="fighter_a_id_1" value="" />
<input type="hidden" name="fighter_b_id_1" id="fighter_b_id_1" value="" />
<input type="hidden" name="winner_id_1" id="winner_id_1" value="" />
<div class="formRow">
<label for="fighter_a_1">Fighters</label>
<input type="text" name="fighter_a_1" id="fighter_a_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_1" id="fighter_b_1" value="" />
</div>
<div class="formRow">
<label for="fighter_a_pay_1">Fighter Pay $</label>
<input type="text" name="fighter_a_pay_1" id="fighter_a_pay_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_pay_1" id="fighter_b_pay_1" value="" />
</div>
<div class="formRow">
<label for="winner_1">Winner</label>
<input type="text" name="winner_1" id="winner_1" value="" />
</div>
<div class="formRow">
<label for="method_1">Method</label>
<input type="text" name="method_1" id="method_1" value="" />
</div>
<div class="formRow">
<label for="method_type_1">Method Type</label>
<input type="text" name="method_type_1" id="method_type_1" value="" />
</div>
<div class="formRow">
<label for="round_1">Round</label>
<input type="text" name="round_1" id="round_1" class="fieldSmall" value="" />
</div>
<div class="formRow">
<label for="time_1">Time</label>
<input type="text" name="time_1" id="time_1" class="fieldSmall" value="" />
</div>
<div class="formRow">
<label for="fight_number_1">Fight #</label>
<input type="text" name="fight_number_1" id="fight_number_1" class="fieldSmall" value="" />
</div>
</div>
<div class="formRowRepeatingSection">
<a href="#" class="buttonGray buttonRight addFight">Add Fight</a>
</div>
<div class="formRow">
<input type="submit" class="submitButton" value="Save Fights" />
</div>
</form>
JS
// Add a new repeating section
$('.addFight').click(function(){
var lastRepeatingGroup = $('.repeatingSection').last();
lastRepeatingGroup.clone().insertAfter(lastRepeatingGroup);
return false;
});
// Delete a repeating section
$('.deleteFight').click(function(){
$(this).parent('div').remove();
return false;
});