我还是PHP和JS的新手,但是从我一直在阅读的内容来看,当一个表单有一个 onSubmit 元素时,表单需要来自函数的return true
为了执行提交。
但是我不能让它工作。
有人可以帮我弄明白我的问题是什么吗?我还在Javascript函数的开头添加了一个随机alert()
,但也没有。{/ p>
谢谢!
<HTML>
<HEAD>
<script type="text/javascript">
function Validate(){
alert("validating..");
var min=document.forms["myForm"]["minWeight"].value;
var max=document.forms["myForm"]["maxWeight"].value;
if (min < max)
{
return true;
}
else
{
alert("Minimum weight " + min + " is not lower than " + max " Maximum weight");
return false;
}
}
</script>
</HEAD>
<BODY>
<form name="myForm" action="page2.php" method="post" onSubmit="return Validate()">
<p><label>Enter minimum weight: <input type="number" name="minWeight" value="0" min="0" required /></label></p>
<p><label>Enter maximum weight: <input type="number" name="maxWeight" value="999" min="0" max="999" required /></label></p>
<p>Select gender:
<label><input type="radio" name="genderType" value="male" required /> Male</label>
<label><input type="radio" name="genderType" value="female" /> Female</label>
</p>
<p><input type="submit" value="Submit" /></p>
</form>
</BODY>
</HTML>
答案 0 :(得分:2)
您的javascript中有拼写错误:
alert("Minimum weight " + min + " is not lower than " + max " Maximum weight");
^^^
将其更改为:
alert("Minimum weight " + min + " is not lower than " + max + " Maximum weight");
它应该有效,请参阅example on jsbin。