第一次在这里发帖,最后的替代方法是对这种胡说八道感到更加沮丧。我认为此代码段没有任何问题,但即使我将字段留空(应当场验证),onsubmit 条件似乎总是返回为真。
注意:我的 JS 函数目前是空白的,还没有走那么远。
进一步调查说明:即使我写的最后一个案例也不起作用,它只是试图在我点击提交后立即到达结帐页面并完全忽略 onsubmit。
<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Book registration. </title>
<script type = "text/javascript" src = 'Script/main.js'></script>
</head>
<body>
<form method = "POST" action = "Checkout" onsubmit = "validate(this)">
<fieldset>
<legend> Book Registry Access </legend>
<p> ISBN: <input type = "text" size = "14" name = "ISBN" onchange = "if(this.value != '') ajaxVerif('checkISBN', this.value, this.id);"> </p>
<p> Title: <input type = "text" size = "32" id = "title" name = "title"> </p>
<p> Author: <input type = "text" size = "32" id = "author" name = "author"> </p>
<p> Edition: <input type = "text" size = "2" id = "edition" name = "edition" onchange = "if(this.value != '') ajaxVerif('checkEdi', this.value, this.id);"> </p>
<p> <input type = "submit"> </p>
</fieldset>
</form>
</body>
</html>
function validate(thisForm)
{
if (thisForm.ISBN.value == '') {
alert('Please enter a valid ISBN number in the form XXXX-YYYY-ZZZZ');
this.ISBN.focus();
return false;
}
if (thisForm.title.value == '') {
alert('Please fill in the title field.');
this.title.focus();
return false;
}
if (thisForm.author.value == '') {
alert('Please fill in the author field.');
this.author.focus();
return false;
}
if (thisForm.edition.value == '') {
alert('Please enter a valid edition number.');
this.edition.focus();
return false;
}
alert('Success! This test form is working! (TO BE DELETED!!)');
return false;
}
编辑:为清洁而编辑,仍然无法正常工作。所以现在一切都在验证方面起作用,显然除了返回的错误。它总是会尝试跳转到下一页(并且因为链接到一个虚拟页面而失败)。我不太清楚为什么,很确定我所有的回报都应该没问题。
最终编辑:已修复,我忘记将其写为 onsubmit = 'return validate(this)'>