Tizen Javascript function undefined: Uncaught ReferenceError: checkToggle is not defined

Tizen Javascript function undefined: Uncaught ReferenceError: checkToggle is not defined

我正尝试在 gear s2 的 tizen Web 应用程序中向我的服务器发出一个简单的 http 请求。

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
    <title>Wearable UI</title>
    <link rel="stylesheet"  href="../../lib/tau/wearable/theme/default/tau.min.css">
    <link rel="stylesheet"  href="../../css/style.css">        
    <!--<script type="text/javascript" src="toggle.js"></script>-->
    <script type="text/javascript" >
function checkToggle(name){
    //make box2 = box1 when checked              
           var checkbox = document.getElementById(name);
           if (checkbox.checked == 1){
               HTTPReq('http://secret.nl/WebServer/edit.php?name='+name+'&value=1');
              console.log("set "+name+" ON");
           }else{
               HTTPReq('http://secret.nl/WebServer/edit.php?name='+name+'&value=0');
               console.log("set "+name+" OFF");
           }
}    
function HTTPReq(theUrl){
    console.log('httpReq');
    var client = new XMLHttpRequest();
    client.open('GET', theUrl);
    client.send();
    }        
</script>
</head>
<body>
    <div class="ui-page" data-enable-page-scroll="false">
        <div class="ui-content">
            <div class="ui-switch">
                <div class="ui-switch-text">
                Led001
                </div>
                <div class="ui-toggleswitch ui-toggleswitch-large">
                        <input type="checkbox" class="ui-switch-input" id="Led001" onclick="checkToggle('Led001')">
                    <div class="ui-switch-button"></div>
                </div>
                <div class="ui-switch-sub-text">
                    Bedroom Light
                </div>
            </div>
        </div>
        <script src="controls.js"></script>
    </div>
</body>
<script type="text/javascript" src="../../lib/tau/wearable/js/tau.min.js"></script>

</html>

当我模拟这个时,我收到错误:未捕获的引用错误:未定义 checkToggle。当我处于网络模拟器模式和实时编辑时,我如何保存相同的文件。代码有效……? 谁能解释一下并告诉我如何解决这个问题。谢谢

尝试:

document.getElementById("Led001").addEventListener("click", function(){
   checkToggle("Led001")
})

并从 HTML

中删除 onClick

我不是 100% 确定我是如何修复它的,但这是有效的代码:

( function () {

    var led001Button = document.getElementById("Led001"),
        led002Button = document.getElementById("Led002");   

    function httpReq(theUrl){
        var xmlhttp = null;
        xmlhttp = new XMLHttpRequest();
//      xmlhttp.onreadystatechange = function(){
//      if (xmlhttp.readyState == xmlhttp.DONE){
//      alert(xmlhttp.responseText);
//      }
//      else{
//      alert(xmlhttp.statusText);
//      }
//      };
//      xmlhttp.onerror = function(e){
//      alert("onerror: " + xmlhttp.statusText);
//      };
        xmlhttp.open("GET", theUrl);
        xmlhttp.send();
    }

    function checkToggle(name){
            //make box2 = box1 when checked              
               var checkbox = document.getElementById(name);
               if (checkbox.checked === true){
                   httpReq('http://secret.nl/WebServer/edit.php?name='+name+'&value=1');
//                console.log("set "+name+" ON");
               }else{
                   httpReq('http://secret.nl/WebServer/edit.php?name='+name+'&value=0');
//                 console.log("set "+name+" OFF");
               }
    }           
    if (led001Button) {
        led001Button.addEventListener("change", function(){
               checkToggle("Led001");
        });
    }
    if (led002Button) {
        console.log('test');
        led002Button.addEventListener("change", function(){
               checkToggle("Led002");
        });
    }
} () );

感谢所有回复,他们都在某种程度上提供了帮助。