我试图在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>
当我模仿这个时,我得到错误:未捕获的ReferenceError:未定义checkToggle。当我在网络模拟器模式和实时编辑中保存相同的文件时。代码有效....? 任何人都可以解释这个并告诉我如何解决这个问题。感谢
答案 0 :(得分:0)
尝试:
document.getElementById("Led001").addEventListener("click", function(){
checkToggle("Led001")
})
从HTML中删除 onClick
答案 1 :(得分:0)
我不是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");
});
}
} () );
感谢所有回复,他们都以某种方式帮助过。