克隆div jQuery UI Datepicker问题

时间:2013-04-26 10:05:41

标签: jquery jquery-ui jquery-ui-datepicker

我有一个包含几个字段的div,可以克隆到另一个div中。其中一个字段使用datepicker ui。但是,当我去克隆div时,datepicker停止为克隆输入工作。我使用并编辑了this question中的以下内容但没有运气

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); });
});

});

3 个答案:

答案 0 :(得分: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();
});

Here's a simplified example fiddle


修改

我了解您现在已添加完整代码的问题。在您的问题中,您没有提到您已将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[]');

Here's another fiddle

答案 1 :(得分:0)

尝试在追加元素后调用datepicker

clone.appendTo('#child').datepicker();

答案 2 :(得分:0)

选中fiddle

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类&amp;克隆上的数据,然后附加它。 这是工作。检查这是否是必需的。