2014-09-22 28 views
0

讀ToggleSwitch值我想讀如果選擇一個值或另一個(1或0,TRUE或FALSE)在通過使用ToggleSwitch WinJS:如何WinJS

代碼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ódigode biblioteca de JavaScriptestáa punto de detectar la excepción。恩拉利內阿119,columna 9 EN MS-APPX://.../js/default.js

0x800a138f - 誤差en蒂恩波德ejecución德的JavaScript:沒有SE puede obtener LA propiedad '的toString' 去referencia nula o sin sin definir

Si hay un controlador para estaexcepción,el programa puede continuar de forma segura。

當我將鼠標懸停在ToggleSwitch代碼上時發現「undefined」的值,我做錯了什麼?

沒有的console.log線ToggleSwitch正確顯示在屏幕上,但我也想要得到它的價值,任何幫助,我會感激

回答

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); 
       }); 
      })); 
+0

完美的作品! !謝謝 – Jesus 2014-09-22 19:31:46

+0

我從這裏參考了http://bubblogging.wordpress.com/2013/02/17/winjs-controls-toggleswitch/ – 2014-09-22 19:32:36