JavaScript判断表单为空及获取焦点的方法


本文实例讲述了JavaScript判断表单为空及获取焦点的方法。分享给大家供大家参考,具体如下:

<%@ 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>

希望本文所述对大家JavaScript程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3