我在這裏叫的一種形式:表格即使在Javascript驗證失敗後也提交?
<span class="aligncenter button"><a href="#" onclick="submitForm('newIncident');">Submit</a></span>
和我有一個JavaScript函數在這裏:提交時
if (myForm == 'newIncident')
{
var vDemAge = document.forms['newIncident']['demAge'].value;
var vBibNumber = document.forms['newIncident']['bibNumber'].value;
// Run through validations before submitting form
validateTime();
validateDate();
validateAge();
validateGender();
validateLocation();
validateType();
validateDisposition();
if (vDemAge == 'Age' || vDemAge == '') // If Age is not entered, set the value to be blank
{
document.forms['newIncident']['demAge'].value = '';
}
if (vBibNumber == 'Bib #' || vBibNumber == '') // If Bib # is not entered, set the value to blank
{
document.forms['newIncident']['bibNumber'].value = '';
}
document.getElementById(myForm).submit();
}
else
{
document.getElementById(myForm).submit();
}
所以我每個驗證爲我在序列正在調用一個單獨的功能表格。如果我註釋掉「document.getElementById(myForm).submit();」,驗證按預期運行。但是,如果我不註釋它,即使驗證失敗,它也會每次提交。我怎樣才能阻止這種情況發生?
謝謝!編號: 所以這是我正在運行的驗證之一。它們的結構都是一樣的。某處我應該返回布爾真/假?我究竟如何在下面的這個插入?
function validateDisposition()
{
var vIncidentDisposition = document.forms['newIncident']['incidentDisposition'].value;
if (vIncidentDisposition == '')
{
document.forms['newIncident']['incidentDisposition'].className = 'required';
}
else
{
document.forms['newIncident']['incidentDisposition'].className = 'formborder';
}
}
你能展示一些驗證功能嗎? – Fabricator
你在哪裏告訴表單不要提交? – bloodyKnuckles