How can I change the Bootstrap toggle state dynamically?

走远了吗. 提交于 2019-12-12 01:23:19

问题


I'm trying to add new bootstrap toggle switch dynamically but I don't know how to change the state dynamically. Thanks in advance!

$('.btn').on('click', function () {
var status = true;    
    for(var i=0; i<3; i++){
        $('p').after(
            '<input id="newCheckBox" type="checkbox" data-off-text="Male" data-on-text="Female" checked="false" class="newBSswitch">'
        );
        console.log(status);
        $('.newBSswitch').bootstrapSwitch('state', status);
        status = false;
    }
});
<p> <a class="btn btn-lg btn-primary" href="#">Display another Toggle</a></p>

Here is the fiddle: http://jsfiddle.net/icamilo95/8wars2ep/7/


回答1:


I realized I just had to add different Id's to the inputs and change the class .newBSSwitch for every specific Id like this:

$('.btn').on('click', function () {
var status = true;    
    for(var i=0; i<5; i++){
        $('p').after(
            '<input id="newCheckBox'+[i]+'" type="checkbox" data-off-text="Male" data-on-text="Female" checked="false" class="newBSswitch">'
        );
        console.log(status);
        $('#newCheckBox'+[i]).bootstrapSwitch('state', status);
        status = false;
    }
});
 <p> <a class="btn btn-lg btn-primary" href="#">Display another Toggle</a></p>


来源:https://stackoverflow.com/questions/30995578/how-can-i-change-the-bootstrap-toggle-state-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!