Javascript 单击事件后引导切换不起作用

Javascript 单击事件后引导切换不起作用,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我试图通过jquery呈现引导切换开关,但第一次之后它就不起作用了,我不知道该怎么做 $(文档).ready(函数(){ $('#showHideButton')。更改(函数(){ var action=$(this.prop('checked'); $('.container').html(''); $('.container input').bootstrapToggle(); 控制台日志(操作); //如果(行动) //console.log('insideif'); //否则 //co

我试图通过jquery呈现引导切换开关,但第一次之后它就不起作用了,我不知道该怎么做

$(文档).ready(函数(){
$('#showHideButton')。更改(函数(){
var action=$(this.prop('checked');
$('.container').html('');
$('.container input').bootstrapToggle();
控制台日志(操作);
//如果(行动)
//console.log('insideif');
//否则
//console.log('Inside-else');
});
});





如果您正在使用引导,并且在页面中加载了引导css,则应在输入中插入正确的css类,以便将复选框呈现为引导开关

检查与交换机相关的Bootrsap示例(并且,查看class属性和示例中使用的css类):


切换此开关元件
禁用开关元件
您可以在此处找到所有示例:

上面的代码似乎在做一个复选框,它是。下面是一个切换而不是复选框的示例:

$(文档).ready(函数(){
$(“#显示隐藏按钮”)。单击(函数(){
$('#showHideButton').html(''+
'' +
'' +
“动态开关”+
'');
});
});

动态开关:

点击我!

您能试着添加这一行吗

$('showHideButton input')。bootstraptogle()

排队后

$('showHideButton').html('……')

查看此

函数callOnClick(e){
var action=$(e).prop('checked');
console.log('内部'+操作);
如果(行动)
$('.container').html('');
其他的
$('.container').html('');
$('.container input').bootstrapToggle();
}



没有兄弟,应该是引导switch@RajeevSingh-好的,我已经更新了示例,以显示一个开关而不是复选框。谢谢,兄弟,这是可行的,但在此之后,当我切换开关时,事件不会在屏幕上触发switch@CristianV请将代码段括在{}中以获取格式帮助,请参见@Rajeev Singh,欢迎光临。对于第二个问题,我认为您的开关将始终由父div上的onclick事件重新生成。您必须删除父div上的click事件,然后开关才能工作。@RajeevSingh您可以使用此代码,但这不是最好的解决方案
函数callOnClick(e){console.log($(e).prop('checked');}$(document)。准备好了吗(function(){$('#showHideButton').change(function(){$('.container').html('
');$('.container input').bootstraptogle();var action=$(this.prop('checked');console.log(action);})再次感谢兄弟我修改了一点代码,然后代码就完全正常了。你可以在下面的评论部分查看
<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" id="customSwitch1">
  <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>
<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
  <label class="custom-control-label" for="customSwitch2">Disabled switch element</label>
</div>