在Javascript中验证单选按钮

时间:2015-03-11 10:00:30

标签: javascript html

我正在尝试使用Javascript验证一些单选按钮,但我无法让它工作。我只想确保用户通过检查单选按钮来回答问题,并且不回答任何问题。这是代码:

Html
….
Gender:
<input type="radio" id="Male" name="Gender"> Male
<input type="radio" id="Female" name="Gender"> Female

Member:
<input type="radio" id="Mem" name="Member"> Yes
<input type="radio" id="NonMem" name="Member"> No

Vegetarian:
<input type="radio" id="Veg" name="Veget"> Yes
<input type="radio" id="NonVeg" name="Veget"> No
……

function validate()

if (!document.getElementByID("Male").checked && !document.getElementByID("Female").checked)
        {alert("Please specify gender");
        return;}

if (!document.getElementByID("Mem").checked && !document.getElementByID("NonMem").checked)
        {alert("Please specify member");
        return;}

if (!document.getElementByID("Veg").checked && !document.getElementByID("NonVeg").checked)
        {alert("Please specify vegetarian");
        return;}

非常感谢任何答案/帮助。

4 个答案:

答案 0 :(得分:1)

function validate()
{
if (!document.getElementById("Male").checked && !document.getElementById("Female").checked)
        {alert("Please specify gender");
        return;}

if (!document.getElementById("Mem").checked && !document.getElementById("NonMem").checked)
        {alert("Please specify member");
        return;}

if (!document.getElementById("Veg").checked && !document.getElementById("NonVeg").checked)
        {alert("Please specify vegetarian");
        return;}
  alert("successfully validated");
  }
Gender:
<input type="radio" id="Male" name="Gender"> Male
<input type="radio" id="Female" name="Gender"> Female
<br>
Member:
<input type="radio" id="Mem" name="Member"> Yes
<input type="radio" id="NonMem" name="Member"> No
<br>
Vegetarian:
<input type="radio" id="Veg" name="Veget"> Yes
<input type="radio" id="NonVeg" name="Veget"> No
<br>
<input type='button' value='Validate' onclick='validate()'>

document.getElementByIDdocument.getElementById

答案 1 :(得分:0)

试试这个

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
 Gender:
    <input type="radio" id="Male" name="Gender"> Male
    <input type="radio" id="Female" name="Gender"> Female
    <br>
    Member:
    <input type="radio" id="Mem" name="Member"> Yes
    <input type="radio" id="NonMem" name="Member"> No
    <br>
    Vegetarian:
    <input type="radio" id="Veg" name="Veget"> Yes
    <input type="radio" id="NonVeg" name="Veget"> No
    <br>
    <input type='button' value='Validate' onclick='validate();'>
    <script>
     function validate()
    {
    if (!document.getElementById("Male").checked && !document.getElementById("Female").checked)
            {alert("Please specify gender");
            return;}

    if (!document.getElementById("Mem").checked && !document.getElementById("NonMem").checked)
            {alert("Please specify member");
            return;}

    if (!document.getElementById("Veg").checked && !document.getElementById("NonVeg").checked)
            {alert("Please specify vegetarian");
            return;}
      alert("successfully validated");
      }
    </script>
</body>
</html>

答案 2 :(得分:0)

你弄乱了小代码错误但是你的逻辑是完全正确的...只需看看你的代码

您使用document.getElementByID它可能是document.getElementById ...你明白了吗?

不是ID。它是Id

答案 3 :(得分:0)

简单,只需更正您的Javascript代码中的&#34; TypeError&#34;

替换:

document.getElementByID 

使用:

document.getElementById