我创建了这个函数,它检查多个文本框是否具有 onsubmit 值。基本上它是一个 javascript 表单验证器。当有空文本框时,表单不应提交并提醒用户有必填字段。现在,这对我来说非常有用,但是,当已经有值并且提交了表单时,即使应该提交,它仍然没有提交。我创建了一个 if 语句来检查 errorString 是空还是 null,如果是,则表单应该提交,但会发生什么是它用空字符串提醒用户。我认为代码仍在 if(errorString!=null || errorString=="") 语句中,即使它不应该。
提前致谢
请在下面查看我的代码:
function validateForm()
{
var txtTitle = document.forms["actionStepsForm"]["txtTitle"].value;
var txtRequestor = document.forms["actionStepsForm"]["txtRequestor"].value;
var txtReprocessingRequest = document.forms["actionStepsForm"]["txtReprocessingRequest"].value;
document.getElementById('rowCount').value = counter-1;
var errorString = "";
if (txtTitle==null || txtTitle=="")
{
errorString += "Title field is required. \n";
}
if (txtRequestor==null || txtRequestor=="")
{
errorString += "Requestor field is required. \n";
}
if (txtReprocessingRequest==null || txtReprocessingRequest=="")
{
errorString += "Reprocessing request FR is required. \n";
}
if (errorString!=null || errorString!="")
{
alert(errorString);
return false;
}
else
{
return true;
}
}
//implementation if HTML form
<form name="actionStepsForm" id="actionStepsForm" action="add_reprocessing.php?action=add" method="POST" onsubmit="return validateForm()">