gpt4 book ai didi

javascript:未选中时复选框值保持为 1 而不是 0

转载 作者:行者123 更新时间:2023-11-30 13:26:48 24 4
gpt4 key购买 nike

嗨,我最近在学习 phonegap。我在下面有这段代码。我的目的是从 mysql 中获取用户记录,然后出现一些用户可以更新的复选框,然后直接写入 mysql。

这个过程运行良好,有趣的是复选框的值,即使我取消选中它,它仍然保持为 1 而不是 0

请指教我哪里出错了?仅供引用,我也是 javascript 的新手,所以很可能只是 js 错误导致了这一点。

<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Ajax Sample</title>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript">
function appReady(){
var myId = sessionStorage.getItem('myId');

var ajax = new XMLHttpRequest();
ajax.open("GET","domain/system_setup.php?user_id="+myId+"",true);
ajax.send();

ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
eval('var data = ' + ajax.responseText + ';');
var theResults = data.dataset;
var theHtml = '';
for(var i=0;i<theResults.length;i++){
if (theResults[i].system_1 == 1) {
var theHtml = theHtml + '<input name="system_1" type="checkbox" id="system_1" value="1" checked="checked" />MS Excel';
} else {
var theHtml = theHtml + '<input name="system_1" type="checkbox" id="system_1" value="0" />MS Excel';
}
if (theResults[i].system_2 == 1) {
var theHtml = theHtml + '<BR><input name="system_2" type="checkbox" id="system_2" value="1" checked="checked" />MYOB';
} else {
var theHtml = theHtml + '<BR><input name="system_2" type="checkbox" id="system_2" value="0" />MYOB';
}
if (theResults[i].system_3 == 1) {
var theHtml = theHtml + '<BR><input name="system_3" type="checkbox" id="system_3" value="1" checked="checked" />Quickbooks';
} else {
var theHtml = theHtml + '<BR><input name="system_3" type="checkbox" id="system_3" value="0" />Quickbooks';
}
if (theResults[i].system_4 == 1) {
var theHtml = theHtml + '<BR><input name="system_4" type="checkbox" id="system_4" value="1" checked="checked" />Xero';
} else {
var theHtml = theHtml + '<BR><input name="system_4" type="checkbox" id="system_4" value="0" />Xero';
}
if (theResults[i].system_5 == 1) {
var theHtml = theHtml + '<BR><input name="system_5" type="checkbox" id="system_5" value="1" checked="checked" />SAP';
} else {
var theHtml = theHtml + '<BR><input name="system_5" type="checkbox" id="system_5" value="0" />SAP';
}
if (theResults[i].system_6 == 1) {
var theHtml = theHtml + '<BR><input name="system_6" type="checkbox" id="system_6" value="1" checked="checked" />Navision';
} else {
var theHtml = theHtml + '<BR><input name="system_6" type="checkbox" id="system_6" value="0" />Navision';
}
if (theResults[i].system_7 == 1) {
var theHtml = theHtml + '<BR><input name="system_7" type="checkbox" id="system_7" value="1" checked="checked" />Others';
} else {
var theHtml = theHtml + '<BR><input name="system_7" type="checkbox" id="system_7" value="0" />Others';
}

}
}
document.getElementById('main').innerHTML = theHtml;
}
}

function updateSystem (form) {
var system_1 = form.system_1.value;
var system_2 = form.system_2.value;
var system_3 = form.system_3.value;
var system_4 = form.system_4.value;
var system_5 = form.system_5.value;
var system_6 = form.system_6.value;
var system_7 = form.system_7.value;

var ajax = new XMLHttpRequest();
ajax.open("GET","domain/system_setup_update.php?user_id="+sessionStorage.getItem('myId')+"&system_1="+system_1+"&system_2="+system_2+"&system_3="+system_3+"&system_4="+system_4+"&system_5="+system_5+"&system_6="+system_6+"&system_7="+system_7+"",true);
ajax.send();

ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
eval('var data = ' + ajax.responseText + ';');
var theResults = data.dataset;
for(var i=0;i<theResults.length;i++){
var theRefresh = window.location = "system_setup.html";
}
document.getElementById('refresh').innerHTML = theRefresh;
}
}
}
document.addEventListener("deviceready", appReady, false);

</script>

<style type="text/css">
.line {padding-bottom:2px;}
.title {float: left; width: 80px;}
.content {margin-left: 90px; }
.clear{clear:both;}
</style>
</head>
<body>
<h1>System Used</h1>

<FORM NAME="myform" ACTION="" METHOD="GET">
<div id="main">
</div>
<INPUT TYPE="button" NAME="button" Value="Update" onClick="updateSystem(this.form)">
</FORM>
<div id="refresh">
</div>
</body>
</html>

提前致谢!

最佳答案

您必须检查项目的 checked bool 属性而不是 value。在您的用例中,value 没有实际意义,因为它会保留您在创建标记时提供的任何内容,并且无论您点击复选框多少次都不会改变。

关于javascript:未选中时复选框值保持为 1 而不是 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8217463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com