简体   繁体   中英

onInit callback of bootstrap switch to check initial state of element

I need to check the current state of a particular bootstrap switch element on page-load and wanted to solve that with the onInit-Callback. Unfortunately it gives mit "undefined" when I ask for the current state. Here is my code so far...

$(".make-switch").bootstrapSwitch({
onText:'ja',
offText:'nein',
onColor:"success",
offColor:"warning",
indeterminate: true,
size:"big",
onInit:function(event,state)
    {
    var current_element=$(this).attr("id");
        if(current_element=="has-emotion")
        {

        alert(state); //gives me "undefined" for this and every other element


        }});

My Bootstrap-Switch Version is v3.3.2 and Jquery Version 2.1.4

What am I doing wrong here? Or is there an other way to check the state of the given element when loading the page?

Thank you very much! Regards from Austria

You can do this like :

$('.make-switch').bootstrapSwitch({        
    onInit:function (event, state) {                        
        alert($(this).is(':checked'));
     }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM