我有一大堆javascript我试图用来验证我的表单。验证正确进行并阻止提交表单,但不显示警告消息,其中包含阻止提交的错误。通过反复试验,我发现了 我可以在检查时间是否为空之前使用警报,之后不能。任何人都可以解释原因吗?
function validateForm()
{
var good = true;
var errorMessage = "";
var name=document.forms["myForm"]["name"].value;
var startDate = document.forms["myForm"]["date"].value;
if (name==null || name=="")
{
errorMessage = errorMessage + "Name must not be empty.\n";
good = false;
}
if(startDate==null || startDate=="" || checkdate(startDate))
{
errorMessage = errorMessage + "Start date is invalid.\n";
good = false;
}
alert("meow"); //Will happen
if(time==null || time=="")
{
errorMessage = errorMessage + "Start time must be filled out\n";
good = false;
}
alert("meow"); //Will not happen
if(time2==null || time2=="")
{
errorMessage = errorMessage +"End time must be filled out\n";
good = false;
}
if(!checkAfter(time, time2))
{
if(!confirm("Since the end time is after the start time, this competition will end tommorow. Continue?"))
{
good = false;
}
}
alert(errorMessage); //will not happen
alert("meow"); // will not happen
return good;
}
答案 0 :(得分:2)
由于没有time
,尝试将其与null
进行比较的if语句会引发引用错误,从而终止脚本执行。
我猜你忘记从表单中获取值并首先将其分配给time
变量。