0

我正在尝试添加动态复选框,但添加后我无法在点击时检查它们

大家好,我正在使用 formValidation.min.js 动态添加输入字段,即复选框,其中我成功添加了复选框,但不幸的是,我无法检查它们或切换任何动态创建的复选框的选中状态。谁能帮我解决这个问题!

提前致谢!

这是我的一段代码

<form action="" id="createQuestionForm" enctype="multipart/form-data">
<div class="input-group" style="margin-bottom: 3px;">
                                            <input type="text" name="item[0][objective]" class="form-control">
                                            <span class="input-group-addon">
                                                <input type="checkbox" value="1" name="item[0][status]">
                                            </span>
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
                                            </span>
                                        </div>
                                        <!-- Template -->
                                        <div class="input-group hide" id="bookTemplate" style="margin-bottom: 3px;">
                                            <input type="text" name="objective" class="form-control">
                                            <span class="input-group-addon">
                                                <input type="checkbox" value="2" class="form-control checkbox-status" name="status">
                                            </span>
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default removeButton"><i class="fa fa-minus"></i></button>
                                            </span>
                                        </div>
</form>

Javascript


$(document).ready(function() {
    bookIndex = 0;

    $('#createQuestionForm')
       // Add button click handler
       .on('click', '.addButton', function() {
            bookIndex++;
            var $template = $('#bookTemplate'),
               $clone    = $template
                                .clone()
                                .removeClass('hide')
                                .removeAttr('id')
                                .attr('data-book-index', bookIndex)
                                .insertBefore($template);

            // Update the name attributes
            $clone
               .find('[name="objective"]').attr('name', 'item[' + bookIndex + '][objective]').end()
               .find('[name="status"]').attr('name', 'item[' + bookIndex + '][status]').end();
       })

       // Remove button click handler
       .on('click', '.removeButton', function() {
            var $row  = $(this).parents('.input-group'),
               index = $row.attr('data-book-index');

            // Remove fields
            $('#bookForm')
               .formValidation('removeField', $row.find('[name="item[' + index + '][objective]"]'))
               .formValidation('removeField', $row.find('[name="item[' + index + '][status]"]'));

            // Remove element containing the fields
            $row.remove();
       });
});

任何人都知道如何使克隆的复选框成为可检查的?

4

0 回答 0