长话短说,我试图了解如何正确使用 onsubmit。testresults 函数可以在 onclick 上正常工作,但我无法在提交时激活它(使用按钮或按 Enter 键)。我在这里错过了什么吗?最终,我只想让 onsubmit 运行检查并提供与 onclick 相同的边界更改/警报。这不可能吗?
function testResults(form) {
var TestVar = form.firstname.value;
if (TestVar.length > 0) {
alert("You typed: " + TestVar);
document.getElementById('firstname').style.border = ''
} else {
alert("You didn't typed!" + TestVar);
document.getElementById('firstname').style.border = '1px solid red';
return false;
}
return true;
}
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="note.css">
<script type="text/javascript" src="formJS2.js"></script>
</head>
<body>
<form NAME="myform" ACTION="" METHOD="GET" onSubmit="testResults();"> <span>First name:</span>
<input type="text" name="firstname" id="firstname" value="" />
<br>
<input type="submit" value="Submit">
<input type="button" name="button" value="Click" onClick="testResults(this.form)">
</form>
</body>
</html>