bug之bootstrap switch Uncaught TypeError: Cannot read property 'apply' of undefined
2019獨角獸企業重金招聘Python工程師標準>>>
<input type="checkbox" name="my-switch" checked>
js寫以下代碼,不然switch控件出不來
$("[name='my-switch']").bootstrapSwitch();
$('[name="my-switch"]').bootstrapSwitch({??
?????????onText:"啟動",??
????????offText:"停止",??
????????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);} }使用上述的方法
$('#toggle-state-switch').bootstrapSwitch('setState', false); 是有問題的。
應該使用
$('#toggle-state-switch').bootstrapSwitch('state', true/false);
還有一個坑,就是在使用外面的div的id設置bootstrapSwitch的state,會多了節點,如下:
這并不是我們想要的結果,所以,設置外置div的值是不對的。應該取checkbox的id設置,也就是,如下:
$('#toggle').bootstrapSwitch('state', false);
對bootstrap switch進行賦值的時候,提示錯誤?
Uncaught TypeError: Cannot read property 'apply' of undefined
詳細錯誤信息如下圖所示:?
?
我參考這個設置 使用jQuery獲取Bootstrap Switch的值?
看來有問題,于是查看源碼,根本沒有setState方法,應該使用下面的方法:
轉載于:https://my.oschina.net/maojindaoGG/blog/1612711
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的bug之bootstrap switch Uncaught TypeError: Cannot read property 'apply' of undefined的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两个Long类型真的不能直接用或比较么?
- 下一篇: iOS之coreData