Так что я использую 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>