3

我有一个 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); });
});

});

4

3 回答 3

4

有几件事供您查看:

  • 看起来您的第二个代码块在$(document).ready(function()).
  • var myDiv是你的按钮,它没有孩子。
  • 当您的输入被克隆时,您会得到重复child_dob的 id。

至于让 datepicker 处理克隆的输入,这应该可以解决问题:

$('#add_child').click(function() {  
    var clone = $('.add-child:first').clone(true).appendTo('#child');
    clone.find('.child_dob').datepicker();
});

这是一个简化的示例小提琴


编辑

既然您已经添加了完整的代码,我了解您的问题是什么。在您的问题中,您没有提到您datepicker已经分配给原始.child_dob. 有点乱,但是你需要datepicker从克隆的元素中删除,然后重新添加:

换行:

clone.find(".child_dob").attr('name', 'child_dob_additional[]');

和:

clone.find(".child_dob").removeClass('hasDatepicker').removeAttr('id')
                        .datepicker().attr('name', 'child_dob_additional[]');

这是另一个小提琴

于 2013-04-26T10:17:44.867 回答
0

在添加元素后尝试调用 datepicker

clone.appendTo('#child').datepicker();
于 2013-04-26T10:08:35.493 回答
0

检查这个小提琴

var clone = $('.add-child input:first').removeClass('hasDatepicker')
  .removeData('datepicker').clone();
        var newCloneId="child_dobx_"+(child_count+1);
        clone.attr("name",newCloneId).attr("id",newCloneId).appendTo('#add-child');

我尝试删除克隆上的 datepicker 类和数据,然后附加它。这是工作。检查这是否是必需的。

于 2013-04-26T11:08:00.897 回答