我有 JavaScript 可以很好地检查文本字段和复选框,但是当涉及单选按钮时,它无法区分是否选择了其中一个选项。
如何正确执行此操作?
这就是我所拥有的:
validateBugForm = function()
{
var name = document.forms["bug_form"]["Name"].value;
var email = document.forms["bug_form"]["Email"].value;
var tos = document.forms["bug_form"]["Accept"].checked;
var project = document.forms["bug_form"]["project"].value;
if (name == "")
{
alert("You must provide your name or nick name!");
return false;
}
if (project == "")
{
alert("You must choose a project.");
return false;
}
if (email.indexOf ('@', 0) == -1 || email.indexOf ('.', 0) == -1)
{
alert("You must provide your email address!");
return false;
}
if (!tos)
{
alert("You must agree to our Terms of Services.");
return false;
}
}
在我的表格中,我有:
<tr>
<td width="30%">Choose a project in which you encountered a problem.
<big><font color="red">*</font></big></td></td>
<td width="10px" class="table_td_space"></td>
<td width="70%">
<input type="radio" name="project" value="1" id="1">1<br>
<input type="radio" name="project" value="2" id="2">2<br>
<input type="radio" name="project" value="3" id="3">3<br>
<input type="radio" name="project" value="4" id="4">4<br>
</td>
</tr>