当前位置: 代码迷 >> 综合 >> 前端 -> JavaScript 表单验证(onsubmit)
  详细解决方案

前端 -> JavaScript 表单验证(onsubmit)

热度:26   发布时间:2023-12-16 09:36:22.0

表单验证(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>
  相关解决方案