---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
下面是一个复选框全选 的例子:
<script type="text/javascript">
function selectAll()
{
var quan = document.getElementById("quan");
var zi = document.getElementsByName("zi");
if(quan.checked)
{
for(var i = 0; i < zi.length; i++)
{
zi[i].checked = true;
}
}
else
{
for(var i = 0; i < zi.length; i++)
{
zi[i].checked = false;
}
}
}
</script>
</head>
<body>
quan <input type = "checkbox" id = "quan" onclick = "selectAll()"><br>
<input type = "checkbox" name = "zi"><br>
<input type = "checkbox" name = "zi"><br>
<input type = "checkbox" name = "zi"><br>
<input type = "checkbox" name = "zi"><br>
<input type = "checkbox" name = "zi"><br>
<input type = "checkbox" name = "zi"><br>
</body>
下面是一个比较好的服务器端验证:
public class jsYanzheng extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
String repassword = req.getParameter("repassword");
//注意这个地方比较好, 把所有的错误信息都放在List里面,到时只需要判断List集合是否为空就可以了。
List list = new ArrayList();
if(username != null || username.length() == 0)
{
list.add("用户名不能为空");
}
if(password.length() == 0)
{
list.add("密码不能为空");
}
if(repassword.length() == 0)
{
list.add("重复密码不能为空");
}
if(password.length() != 0 && repassword.length() != 0 && !password.equals(repassword))
{
list.add("密码不一致");
}
if(list.size() == 0)
{
req.setAttribute("username", username);
req.setAttribute("password", password);
req.getRequestDispatcher("oneday/jsYanzhengsuccess.jsp").forward(req, resp);
}
if(list.size() != 0)
{
System.out.println("error");
req.setAttribute("list", list);
req.getRequestDispatcher("oneday/jsYanzhengerror.jsp").forward(req, resp);
}
}
}
这种方式非常好,特别是那个List应该学习。要判断是否为空,否则从地址栏访问时会抛异常。