我正在使用 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.   Gestational age in weeks  
<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.   PRBC transfusion  
<input type="radio" id="PRBC" name="PRBC" value="Yes"/>Yes
<input type="radio" name="PRBC" value="No"/>No
<br>
3.   Human milk feeding at both day 7 and day 14 of life  
<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 部分不起作用..
最佳答案
这是一个例子
<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>
我只对第一组单选按钮执行了此操作。希望这能给您一个想法。
关于使用 javascript 进行 PHP 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15309980/