2011-03-03 91 views
2

我一直在试图检查复选框是否被选中。出于某种原因,它不能正常工作,它只是说没有选择任何东西。检查复选框是否使用Javascript进行检查

功能

if(document.theform.hail.Checked==true && document.theform.wind.Checked==false && document.theform.tornado.Checked==false){ 
     alert("Hail Checked"); 
}else{ 
     alert("Nothing Selected"); 
    } 

形式

<form name="theform" action="<?php echo $PHP_SELF; ?>" method="post"> 
     <div class="date"> 
     From: <script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script> To:<script type="text/javascript">DateInput('orderdatetwo', true, 'YYMMDD')</script> 
     </div> 
     <div class="checkBoxes"> 
     <input id="hail" name="hail" type="checkbox" value="hail">Hail<br /> 
     <input id="wind" name="wind" type="checkbox" value="wind">Wind<br /> 
     <input id="tornado" name="tornado" type="checkbox" value="tornado">Tornado<br /> 
     <input name="submit" type="submit" value="View Data" onClick="document.theform.action='<?php echo $PHP_SELF; ?>';"> 
     <input name="submit" type="button" value="Create KML" onClick="generatorChoice();"> 
+0

你说风和冰雹必须检查......这是正确的吗? – Zoidberg 2011-03-03 03:18:37

+0

这个测试只是检查冰雹 – shinjuo 2011-03-03 03:19:34

回答

8

的属性应该是在小写。

var theform = document.theform; 
if(theform.hail.checked && !theform.wind.checked && !theform.tornado.checked) { 
    alert("Hail Checked"); 
} else { 
    alert("Nothing Selected"); 
} 

而且,上面的代码所示:

  • 无需对truefalse
  • 商店获得更好的性能
+0

你是指通过存储对表单的引用是什么意思? – shinjuo 2011-03-03 03:22:16

+0

@shinjuo:我的回答中的第一行代码,'var theform = document.theform;' – 2011-03-03 03:23:04

+0

对不起,这是漫长的一天。感谢您的帮助 – shinjuo 2011-03-03 03:24:24

2

明确检查theform参考在Javascript中,该物业的名称为checked,而不是Checked

3
if(document.getElementById("hail").checked && !document.getElementById("wind").checked && !document.getElementById("tornado").checked) { 
    alert("Hail Checked"); 
} else { 
    alert("Nothing Selected"); 
} 
+0

我喜欢使用getElementById好得多。更清洁,更不用说与旧的浏览器,AHEM IE ... AHEM的机会... – Zoidberg 2011-03-03 03:26:41

+1

就我个人而言,我绝不会想到以另一种方式做它。然后,我也几乎永远不会使用jQuery ...... – 2011-03-03 03:29:44

相关问题