Javascript 我如何才能切换“切换”;复选框“;用于动态克隆或生成?
大家好,我正在使用formValidation.min.js动态添加输入字段,即复选框,我成功地添加了复选框,但不幸的是,我无法在动态创建的任何复选框上选中它们或切换选中状态。有人能帮我弄清楚吗 提前谢谢 这是我的一段代码Javascript 我如何才能切换“切换”;复选框“;用于动态克隆或生成?,javascript,jquery,html,checkbox,formvalidation.io,Javascript,Jquery,Html,Checkbox,Formvalidation.io,大家好,我正在使用formValidation.min.js动态添加输入字段,即复选框,我成功地添加了复选框,但不幸的是,我无法在动态创建的任何复选框上选中它们或切换选中状态。有人能帮我弄清楚吗 提前谢谢 这是我的一段代码 <form action="" id="createQuestionForm" enctype="multipart/form-data"> <div class="input-group" style="margin-bottom: 3px;">
<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>
任何对如何使克隆的复选框可检查有想法的人???您显示的代码实际上不足以作为一个完整的示例来重现问题。但是,默认情况下,您应该能够选中/取消选中动态复选框。唯一不起作用的方法是,如果你有一些JS干扰该事件,但你没有显示任何可以做到这一点的东西。实际上,我已经尝试消除了大多数我认为会干扰复选框的JS,但它只发生在复选框上!
$(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();
});
});