MDL: Set Switch state to “on” via JS

*爱你&永不变心* 提交于 2019-12-24 17:28:08

问题


I can access my switch via let iSwitch = document.getElementById('interestedSwitch');

I looked at github and found the method MaterialSwitch.on(), but iSwitch.MaterialSwitch.on(); is undefined. iSwitch.checked = true; does not help either. I am using AngularJS, if this could be a source of the problem.


回答1:


Assuming interestedSwitch is the input element with class mdl-switch__input you need to access the parent element. This should be the label with mdl-js-switch (with MaterialSwitch component). Then you can try something like:

if(parent) {
        if(parent.MaterialSwitch && parent.MaterialSwitch.checkToggleState) {
            /* update MDL state if it was changes form javascript */
            parent.MaterialSwitch.checkToggleState();
        }
    }


来源:https://stackoverflow.com/questions/36099109/mdl-set-switch-state-to-on-via-js

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