Tag Archives: Bootstrap switch Uncaught TypeError

Bootstrap switch Uncaught TypeError: Cannot read property ‘apply’ of undefined

<input type=”checkbox” name=”my-switch” checked>

js write the following code, otherwise the switch control will not come out

$(“[name=’my-switch’]”).bootstrapSwitch();

$(‘[name=”my-switch”]’).bootstrapSwitch({

onText:”run”,

offText:”stop”,

onColor:”success”,

offColor:”info”,

size:”small”,

onSwitchChange:function(event,state){

if(state==true){

$(this).val(“1”);

}else{

$(this).val(“2”);

}

}

})

 

<div class="make-switch" data-on-label="开" data-off-label="关" id="toggle-state-switch" data-on="success" data-off="warning">
    <input id ="toggle" type="checkbox"  checked="checked" class="toggle"  />
</div>

function changeState(stateValue){
        if(stateValue==1){
            $('#toggle-state-switch').bootstrapSwitch('setState', true);
        }else{
            $('#toggle-state-switch').bootstrapSwitch('setState', false);
        }
}

Use the above method

$(‘#toggle-state-switch’).bootstrapSwitch(‘setState’, false); There is a problem

It should be used

$(‘#toggle-state-switch’).bootstrapSwitch(‘state’, true/false);

Another pit is to use the ID of the external div to set the state of the bootstrap switch, and there will be more nodes, as follows:

This is not the result we want, so it is wrong to set the value of external Div. The ID of the check box should be set as follows:

$(‘#toggle’).bootstrapSwitch(‘state’, false);

When assigning a value to the bootstrap switch, an error
uncaught type error: cannot read property 'apply' of undefined is prompted


        if (isUp==undefined || isUp == ''){
            $('#isUp').bootstrapSwitch('setState',true);
        } else{
            $('#isUp').bootstrapSwitch('setState',isUp);
        }

The detailed error information is shown in the following figure:

I refer to this setting and use jQuery to get the value of bootstrap switch
it seems that there is a problem, so I check the source code. There is no setstate method at all. I should use the following method:

if(stateValue==0){
            $('#toggle').bootstrapSwitch('state', true);
            console.log("Status enabled or not 1:",stateValue);
        }else{
            console.log("Status enabled or not 2:",stateValue);
            $('#toggle').bootstrapSwitch('state', false);
        }