我正在使用多个由 JS
添加到页面上的 Bootstrap 4 开关
,并且需要在开关更改时调用函数。以下示例在我将开关添加到 HTML
时有效,但是当我在页面加载后通过 JS
动态加载它们时,它将不起作用。
我不想使用其他外部库来解决这个问题!
<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>
我尝试了多个JS代码,但似乎都没有起作用。例如:
$('.custom-control').on('change', function (e) {
let value = this.value;
let test = $(e).target.checked;
});
我也尝试使用 input
标签的 class
.