如何在WinJS中读取ToggleSwitch值

时间:2014-09-22 18:14:31

标签: winjs toggleswitch

我想通过使用ToggleSwitch读取WinJS中是否选择了一个值或另一个值(1或0,TRUE或FALSE):

代码HTML:

<div id="toggleSwitchDocFormat"  class="toggleSwitchDocFormat" data-win-control="WinJS.UI.ToggleSwitch" data-win-options="{labelOn:'Guardar Documento Como: TIFF', labelOff:'Guardar Documento Como: PDF', checked:true}"></div>

代码Javascript:

app.onloaded = function () {
        getDomElements();

        toggleSwitchDocFormat = document.getElementById("toggleSwitchDocFormat").winControl;
        console.log("the value of the ToggleSwitch: " + toggleSwitchDocFormat.checked);

    }

消息:

  

Elcódigodebiblioteca deJavaScriptestáapunto de detectar la   excepción。 enlínea119,columna 9 en   MS-APPX://.../js/default.js

     

0x800a138f - 错误en tiempodeejecucióndeJavaScript:No se puede   obtener la propiedad'toString'de referencia nula o sin definir

     

Si hay un controlador paraestaexpépción,el programa puede continuar   de forma segura。

当我悬停ToggleSwitch代码时,我发现“未定义”值,我做错了什么?

没有console.log行,ToggleSwitch在屏幕上正确显示,但我也希望得到它的价值,任何帮助我都会感激

1 个答案:

答案 0 :(得分:2)

试试这段代码:

args.setPromise(WinJS.UI.processAll().then(function () {

                document.getElementById("toggleSwitchDocFormat").winControl.addEventListener("change", function switchChanged(e) {
                    var _toggleSwitchDocFormat = e.target.winControl;
                    console.log("is it active??: " + _toggleSwitchDocFormat.checked);
                });
            }));
相关问题