我有一个 div,其中包含一些可以克隆到另一个 div 中的字段。其中一个字段使用 datepicker ui。但是,当我去克隆 div 时,日期选择器停止为克隆的输入工作。我已经使用并编辑了这个问题的以下内容,但没有运气
HTML:
<div class="add-child">
<input type="text" class="child_dob" name="child_dob" id="child_dob" />
</div>
<button type="button" id="add_child">Add another child</button>
jQuery:
/* CLONE CODE */
$(function() {
$('#add_child').click( function(){
var clone = $('.add-child:first').clone();
clone.appendTo('#child');
$(":input").each(function (i) { $(this).attr('tabindex', i + 1); });
});
});
我真的不明白我哪里出错了。任何帮助将不胜感激
编辑:
HTML:
<table class="portrait-table add-child">
<tr>
<th colspan="6"><strong>Children details</strong></th>
</tr>
<tr>
<th>Name</th><td><input type="text" class="child_name" name="child_name" value="<?php echo set_value('child_name'); ?>" /></td><td><?php echo form_error('child_name') ?></td>
</tr>
<tr>
<th>Relationship to you</td>
<td><select class="child_relation" name="child_relation">
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Other">Other</option>
</select>
</td>
<td><?php echo form_error('child_relation') ?></td>
</tr>
<tr>
<th>Age</th><td><input type="text" class="child_age" name="child_age" value="<?php echo set_value('child_age'); ?>" /></td><td><?php echo form_error('child_age') ?></td>
</tr>
<tr>
<th>DoB</th><td><input type="text" class="child_dob" name="child_dob" value="<?php echo set_value('child_dob'); ?>" /></td><td><?php echo form_error('child_dob') ?></td>
</tr>
<tr>
<th>Gender</th>
<td><select class="child_gender" name="child_gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
<td><?php echo form_error('child_gender') ?></td>
</tr>
<tr>
<th>Ethnicity</th>
<td><select class="child_ethnicity" name="child_ethnicity">
<option>Ethnicity</option>
<option value="White British">White British</option>
<option value="White Irish">White Irish</option>
<option value="Traveller">Traveller</option>
<option value="Gypsy/Roma">Gypsy/Roma</option>
<option value="Any other White Background">Any other White Background</option>
<option value="Black Caribbean">Black Caribbean</option>
<option value="Black African">Black African</option>
<option value="Any other Black Background">Any other Black Background</option>
<option value="Indian">Indian</option>
<option value="Bangladeshi">Bangladeshi</option>
<option value="Chinese">Chinese</option>
<option value="Mixed Background">Mixed Background</option>
<option value="Any other Asian Background">Any other Asian Background</option>
<option value="Any Other Ethnic Group">Any Other Ethnic Group</option>
<option value="Not given">Not given</option>
</select>
</td>
<td><?php echo form_error('child_ethnicity') ?></td>
</tr>
<tr>
<th>School</th>
<td>
<select class="child_school" name="child_school">
<option value="Southwick Community Primary School">Southwick Community Primary School</option>
<option value="Valley Road Community Primary School">Valley Road Community Primary School</option>
<option value="Diamond Hall Junior School">Diamond Hall Junior School</option>
<option value="Highfield Primary School">Highfield Primary School</option>
<option value="Usworth Colliery Primary School">Usworth Colliery Primary School</option>
<option value="Hetton Primary School">Hetton Primary School</option>
<option value="Hetton Lyons Primary School">Hetton Lyons Primary School</option>
<option value="Other">Other</option>
<option value="N/A">N/A</option>
</select>
</td>
<td><?php echo form_error('child_school') ?></td>
</tr>
</table>
<div id="child"></div>
<p><button type="button" id="add_child">Add another child</button></p>
jQuery(我需要每个字段名称都附加 _additional):
$(function() {
$('#add_child').click( function(){
var clone = $('.add-child:first').clone(true).appendTo('#child');
clone.find("input").val("");
clone.find(".child_name").attr('name', 'child_name_additional[]');
clone.find(".child_relation").attr('name', 'child_relation_additional[]');
clone.find(".child_age").attr('name', 'child_age_additional[]');
clone.find(".child_dob").attr('name', 'child_dob_additional[]');
clone.find(".child_gender").attr('name', 'child_gender_additional[]');
clone.find(".child_ethnicity").attr('name', 'child_ethnicity_additional[]');
clone.find(".child_school").attr('name', 'child_school_additional[]');
$(":input").each(function (i) { $(this).attr('tabindex', i + 1); });
});
});