<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form id="form1" action=""> <input type="text" name="username" placeholder="请输入用户名"> <button type="submit" name="doSubmit">提交</button> <button type="reset" name="soReset">重置</button> </form> <script type="text/javascript"> var f = document.getElementById('form1'); f.onsubmit = function(){ if(this.username.value == ''){ alert('请输入用户名'); return false; } } f.onreset = function(){ return confirm('你确定要重置?'); } </script> </body> </html>