したがって、jqueryを使用してdivのクローンを作成し、入力配列のサイズを動的に保つことができるようにします。正常に動作していますが、クローンの前にクラスを追加して、クローンの後に古いadd moreリンクを削除し、クローンからクラスを削除した後にremoveを実行すると、より効率的に実行できると考えて仕方がありません。または、おそらくこれを行うための全体的により効率的な方法。
ここにhtmlがあります:
<div class="reg_heading_description" id="bio_brothers"></div>
<div class="bio_brother">
<div class="clearfix">
<label>First Name</label>
<div class="input">
<input type="text" name="bio_brother_first[]" style="float:left"/>
<label>Last Name</label>
<input type="text" name="bio_brother_last[]" style="margin-left:20px;"/>
</div>
</div>
<div class="clearfix">
<a href="#" class="more_bio_brother more_relatives" >Add Another</a>
</div>
</div>
</div>
そしてここにjqueryがあり、そうです、競合なしモードです
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(".more_bio_brother").click(function () {
var here = jQuery(this).closest('div').parent();
var names = jQuery(here).find('input');
//validate
var check = 0;
jQuery.each(names, function () {
if (jQuery(this).val() == "") {
check = 1;
}
});
if (check == 1) {
alert('You must enter a first and last name for this biological brother before you can add another.');
return false;
}
//disable prior
jQuery.each(names, function () {
jQuery(this).attr('readonly', true);
});
//add new
jQuery(here).addClass('old');
jQuery('#bio_brothers').before(jQuery(here).clone(true));
jQuery.each(names, function () {
jQuery(this).attr('readonly', false);
jQuery(this).val("");
});
jQuery(here).removeClass('old');
jQuery('.old a').remove();
return false;
});
});
</script>