The example in this article describes how to use JavaScript to determine whether a form is empty and get focus. Share it with everyone for your reference, the details are as follows:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>javascript获得焦点</title> </head> <script language="JavaScript"> function aa() { if(document.forms[0].elements[0].value.length==0) { alert("姓名不可以为空"); document.forms[0].elements[0].focus(); return false; } else if(document.forms[0].elements[1].value.length==0) { alert("密码不可以为空"); document.forms[0].elements[1].focus(); return false; }else {return true;} } </script> <body> <form action="/ok.jsp" method="post" name="form1" onSubmit="return aa();"> 用户名<input type="text" name="name" > 密码<input type="password" name="password"> <input type="submit" value="提交"> </form> </body> </html>
I hope this article will be helpful to everyone in JavaScript programming.