选中时克隆复选框
Clone checkbox when checked
我希望能够在单击时克隆一个复选框,并且克隆的复选框将被取消选中。选中克隆复选框后,它将克隆自身并重复该过程。
<section class="misc">
<div class="row">
<label class="label"><input type="checkbox" class="others"> Others:</label>
</div>
<div class="row">
<div class="col-md-6"><input type="text" class="form-control" placeholder="Others"/></div>
</div>
</section>
<div class="sth"></div>
<!-- Clone the "Others" checkbox with textbox when the original checkbox is checked. I want each subsequent clone to be unchecked, then clone itself.. -->
<script>
$(".others").change(function(){
if($(this).is(':checked'))
{
var clone=$(this).parents('section').clone();
$('.sth').html(clone);
}
});</script>
现在,我只能克隆原始文件,并且克隆的复选框处于选中状态,而不是未选中状态。
Jsfiddle:http://jsfiddle.net/cLkvxydh/
- 要取消选中新创建的复选框,您可以使用
.find('.others:checkbox').prop('checked' , false).end();
- 如果您需要为动态生成的
checkbox
使用点击事件,您需要使用 $(document).on('change',".others" ,function(){
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="misc">
<div class="row">
<label class="label"><input type="checkbox" class="others"> Others:</label>
</div>
<div class="row">
<div class="col-md-6"><input type="text" class="form-control" placeholder="Others"/></div>
</div>
</section>
<div class="sth"></div>
<!-- Clone the "Others" checkbox with textbox when the original checkbox is checked. I want each subsequent clone to be unchecked, then clone itself.. -->
<script>
$(document).on('change',".others" ,function(){
if($(this).is(':checked'))
{
var clone=$(this).parents('section').clone().find('.others:checkbox').prop('checked' , false).end();
$('.sth').append(clone);
}
});</script>
注意: 通过在 $('.sth').html(clone);
中使用 html()
它会一次又一次地改变整个 html 所以你不会注意到代码的效果.. 所以我将 html(
更改为 append(
让你看到代码是有效的
我希望能够在单击时克隆一个复选框,并且克隆的复选框将被取消选中。选中克隆复选框后,它将克隆自身并重复该过程。
<section class="misc">
<div class="row">
<label class="label"><input type="checkbox" class="others"> Others:</label>
</div>
<div class="row">
<div class="col-md-6"><input type="text" class="form-control" placeholder="Others"/></div>
</div>
</section>
<div class="sth"></div>
<!-- Clone the "Others" checkbox with textbox when the original checkbox is checked. I want each subsequent clone to be unchecked, then clone itself.. -->
<script>
$(".others").change(function(){
if($(this).is(':checked'))
{
var clone=$(this).parents('section').clone();
$('.sth').html(clone);
}
});</script>
现在,我只能克隆原始文件,并且克隆的复选框处于选中状态,而不是未选中状态。
Jsfiddle:http://jsfiddle.net/cLkvxydh/
- 要取消选中新创建的复选框,您可以使用
.find('.others:checkbox').prop('checked' , false).end();
- 如果您需要为动态生成的
checkbox
使用点击事件,您需要使用$(document).on('change',".others" ,function(){
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="misc">
<div class="row">
<label class="label"><input type="checkbox" class="others"> Others:</label>
</div>
<div class="row">
<div class="col-md-6"><input type="text" class="form-control" placeholder="Others"/></div>
</div>
</section>
<div class="sth"></div>
<!-- Clone the "Others" checkbox with textbox when the original checkbox is checked. I want each subsequent clone to be unchecked, then clone itself.. -->
<script>
$(document).on('change',".others" ,function(){
if($(this).is(':checked'))
{
var clone=$(this).parents('section').clone().find('.others:checkbox').prop('checked' , false).end();
$('.sth').append(clone);
}
});</script>
注意: 通过在 $('.sth').html(clone);
中使用 html()
它会一次又一次地改变整个 html 所以你不会注意到代码的效果.. 所以我将 html(
更改为 append(
让你看到代码是有效的