表单验证(onsubmit):先验证再提交到action,如果是false就不提交
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title><script>function validateForm() {
let byId = document.getElementById("#aa");var x = document.forms["myForm"]["fname"].value;if (x == null || x == "") {
alert("需要输入名字。");return false;}}</script>
</head>
<body>
<p id="aa">11</p>
<!--onsubmit提交验证,先验证再提交到action,如果是false就不提交-->
<form name="myForm" action="demo_form.php"onsubmit="return validateForm()" method="post">名字: <input type="text" name="fname"><input type="submit" value="提交">
</form></body>
</html>