可能是一个小错字或其他东西,但我看了看,但我找不到它。我已经看了很多相同的帖子,我仍然无法弄清楚。任何帮助将不胜感激。
<script language="Javascript">
function validateForm()
{
var x = document.addNewForm.ecrNumber.value;
if (x==null || x=="")
{
alert("ECR Number must be filled out");
document.addNewform.ecrNumber.focus();
return false;
}
var y=document.addNewForm.origin.value;
if (y==null || y=="")
{
alert("Originator Name must be filled out");
document.addNewform.origin.focus();
return false;
}
var des=document.addNewform.descript.value;
if (des=null || des=="")
{
alert("Description must be filled out");
document.addNewform.descript.focus();
return false;
}
return true;
}
</script>
和html
<form name="addNewForm" action="index.php/ecr-form-to-database" onSubmit="return validateForm();" method="post">
<table width="60%">
<tr><td>ECR No.: </td><td><input type="textbox" id="ecrNumber" name="ecrNumber" /> </td> </tr>
<tr><td>Originator Name: </td><td><input type="textbox" id="origin" name="origin" value="<?php print $empName;?>" / ></td></tr>
<tr><td>Description:</td><td><textarea id="descript" name="descript"></textarea></td></tr>
<tr><td> </td><td> </td></tr>
<tr><td colspan="2" style="text-align:center"><input type="submit" value="Submit Form" /></td></tr>
</table>
</form>
答案 0 :(得分:2)
并非所有浏览器都直接在表单上存储表单和ID,但由于所有元素都有ID,为什么不使用它:
function validateForm() {
var x = document.getElementById('ecrNumber');
var y = document.getElementById('origin');
var des = document.getElementById('descript');
if ( ! x.value ) {
alert("ECR Number must be filled out");
x.focus();
return false;
}
if ( ! y.value ) {
alert("Originator Name must be filled out");
y.focus();
return false;
}
if ( ! des.value ) {
alert("Description must be filled out");
des.focus();
return false;
}
return true;
}
答案 1 :(得分:0)
您尚未调用函数validateForm。
<input type="submit" onclick="return validateForm();" value="Submit Form" />