引导开关不适用于动态创建的复选框输入

bootstrap switch does not work for dynamically created checkbox input

本文关键字:创建 复选框 输入 动态 适用于 开关 不适用      更新时间:2023-09-26

我有以下脚本组合:jquerybootstrapmodernizerjson2html.

调用类似以下内容:

<script>
   $('#my01Switch').bootstrapSwitch({
            onText: 'ON',
            offText: 'OFF',
            size: 'large',
            onColor: 'success',
            offColor: 'warning',
            labelText: '*'
        });
    </script>

结束正文标签之前,在 :

<input name="my01Switch" id="my01Switch" data-toggle="switch" checked="" type="checkbox">

以上是动态生成的输入标签。

请帮忙。

在执行引导开关插件之前检查文档是否已准备就绪:

$(document).ready(function() {
    //Code
});

更新开关复选框代码:

向复选框添加类

<input name='"my01Switch'" id='"my01Switch'" type='"checkbox'"  data-toggle='"switch'" checked='"true'" class='"custom-box'"/>

更新 js 代码:

function renderRooms(json)
{
    if (json !== undefined) {
        //.....
        //$('#roomBoxWrap').json2html(json, transforms.theRoom);
        $('#form-nav').json2html(json.rooms, transforms.menurooms);
        $('#title_room_name').append(getRoomName(json));
        //After the rooms are loaded
        $('.custom-box').bootstrapSwitch();
    }
}