0

我正在开发一个使用 php 和 mysql 的应用程序。我编写了一个用于表单验证的 javascript 代码,但它可以工作。

     function checkForm()
        { if(document.Form1.Gestational_age.value=="")
                {
                   alert('Please select an option for Gestational age in weeks ') ;
                   return false;
                }
            else if(document.Form1.PRBC.value=="")
                {
                    alert('Please select an option for PRBC transfusion ');
                    return false;
                }
            else if(document.Form1.milk_feeding.value=="")
                {
                    alert('Please select an option for Human milk feeding at both day 7     and day 14 of life');
                    return false;
                }
    }
   </script>

    <title>GutCheckNEC for infants < 1500 grams at birth Gephart,2012</title>

   </head>
   <body>

    <div><b>GutCheckNEC   for infants < 1500 grams at birth Gephart, 2012</b></div>
    <br>
    <form name="Form1" method="post" action ="Form1.php" onSubmit="return checkForm();">
        1.&nbsp;&nbsp;&nbsp Gestational age in weeks&nbsp;&nbsp;&nbsp
         <input type="radio" id="Gestational_age" name="Gestational_age" value="0-27"/>0-27
         <input type="radio" name="Gestational_age" value="28-31"/>28-31
         <input type="radio" name="Gestational_age" value=">32" />32
         <br>

        2.&nbsp;&nbsp;&nbsp PRBC transfusion&nbsp;&nbsp;&nbsp
        <input type="radio" id="PRBC" name="PRBC" value="Yes"/>Yes
         <input type="radio" name="PRBC" value="No"/>No
         <br>
        3.&nbsp;&nbsp;&nbsp Human milk feeding at both day 7 and day 14 of life&nbsp;&nbsp;&nbsp
        <input type="radio" id="milk_feeding" name="milk_feeding" value="Yes"/>Yes
         <input type="radio" name="milk_feeding" value="No"/>No
         <br>

     <input type="submit" name="submit" value="Next"/>
    </form>
    </body>
   </html>

这是我的代码示例。请你帮忙,为什么我的 javascript 部分不起作用..

4

4 回答 4

2

用这个替换验证功能并尝试。这将检查该值是否已设置。

    function checkForm()
    { 
        if(!document.forms["Form1"]["Gestational_age"][0].checked && !document.forms["Form1"]["Gestational_age"][1].checked && !document.forms["Form1"]["Gestational_age"][2].checked)
            {
               alert('Please select an option for Gestational age in weeks ') ;
               return false;
            }
        else if(!document.forms["Form1"]["PRBC"][0].checked && !document.forms["Form1"]["PRBC"][1].checked)
            {
                alert('Please select an option for PRBC transfusion ');
                return false;
            }
        else if(!document.forms["Form1"]["milk_feeding"][0].checked && !document.forms["Form1"]["milk_feeding"][1].checked)
            {
                alert('Please select an option for Human milk feeding at both day 7 and day 14 of life');
                return false;
            }
    }
于 2013-03-09T11:26:02.923 回答
2

这是一个例子

<script>
function checkForm(){ 
if(Form1.Gestational_age[0].checked==false && Form1.Gestational_age[1].checked==false && Form1.Gestational_age[2].checked==false)
   {
      alert('Please select an option for Gestational age in weeks ') ;
      return false;
    }           
   else {
     return true;   
   }
}
</script>

我只为第一组单选按钮做到了这一点。希望这能给你一个想法。

于 2013-03-09T11:39:02.060 回答
1

你不能像这样获得单选按钮的价值:

document.Form1.Gestational_age.value

像这样使用它

var ages = document.getElementsByName('Gestational_age');
var ageIsChecked = false;
for (var i = 0, length = ages.length; i < length; i++) {
    if (ages[i].checked) {
        ageIsChecked = true;
    }
}
if (!ageIsChecked) {
    alert('Please select an option for Gestational age in weeks ');
    return false;
}
于 2013-03-09T11:25:50.217 回答
0

尝试 ,Gestational_age是带有数组的名称,因此您需要传递 as["Form1"]["Gestational_age"]checked属性来检查单选按钮的长度,单选按钮是否被选中。

function checkForm()
{ 
    if(document.forms["Form1"]["Gestational_age"].checked == "")
    {
        alert('Please select an option for Gestational age in weeks ') ;
        return false;
    }
    else if(document.forms["Form1"]["PRBC"].checked == "")
    {
        alert('Please select an option for PRBC transfusion ');
        return false;
    }
    else if(document.forms["Form1"]["milk_feeding"].checked == "")
    {
        alert('Please select an option for Human milk feeding at both day 7     and day 14 of life');
        return false;
    }
}

它会根据需要提醒您的验证,因为我已经测试过了。

于 2013-03-09T11:25:57.753 回答