我正在构建一个表单,该表单应该根据用户所做的引导开关选择来隐藏和显示内容。onclick="documentFilter 事件适用于普通复选框;但是,在我初始化引导开关的那一刻,代码无法按预期工作。知道我缺少什么吗?谢谢!
<!--DEPENDENCIES-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js"></script>
<!--HTML-->
<body>
<div class="container">
<label>
<input type="checkbox" name="my-checkbox" onclick="documentFilter(this, '#hideableDiv')"> Some caption
</label>
<div id="hideableDiv" class="hiddenByDefault">Some hidable content
</div>
</div>
</body>
<!--PAGE-SPECIFIC SCRIPT-->
<script type="text/javascript">
//Initialise BootstrapSwitch
$("[name='my-checkbox']").bootstrapSwitch();
//Unchecked on load
$(".hiddenByDefault").hide();
//document filter function
function documentFilter(trigger, target) {
var $target = $(target);
$(trigger).change(function () {
$target.toggle(this.checked);
});
}
</script>