<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload=function(){ var f=document.getElementById('f'); //输入框获取 var username=document.getElementById('username'); //提交表单事件监听 f.onsubmit=function() { //判断是否存在内容 if(username.value=='') { //没有内容 alert('请填写用户名') } else { //有内容 alert('提交成功') return true;//可以提交表单 } return false;//阻止提交表单 } } </script> </head> <body> <form id="f" action="index.html" method="post"> <input type="text" id="username" value="" /> <input type="submit" value="提交"/> </form> </body> </html>
DOM表单的练习操作
最新推荐文章于 2022-07-20 19:50:47 发布