Posted on 2006-06-28 09:37
多力宇扬 阅读(522)
评论(0) 编辑 收藏 所属分类:
Script
checkbox全选实现
function CheckOthers(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
// if (e.name != 'chkall')
if (e.checked==false)
{
e.checked = true;// form.chkall.checked;
}
else
{
e.checked = false;
}
}
}
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
// if (e.name != 'chkall')
e.checked = true// form.chkall.checked;
}
}
建立表单对象,表单名:name=form,并建立checkbox,命名:name="checked"
建立全选按钮,代码:
<INPUT title=选择所有的Banner,点击选定按钮 onclick=CheckAll(this.form) type=button value=全选 name=chkall style="cursor:hand">
建立反选按钮,代码:
<INPUT title=反向选择所有的Banner,点击选定按钮 onclick=CheckOthers(this.form) type=button value=反选 name=chkOthers style="cursor:hand">
完整代码:
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Dory.Doo-测试JSTL</title>
<script language="javascript">
<!--
function CheckOthers(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
// if (e.name != 'chkall')
if (e.checked==false)
{
e.checked = true;// form.chkall.checked;
}
else
{
e.checked = false;
}
}
}
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
// if (e.name != 'chkall')
e.checked = true// form.chkall.checked;
}
}
-->
</script>
</head>
<body>
<h2>体育运动种类</h2>
<hr>
<form action="test.jsp" method="post" name="form">
<input type="checkbox" name="sports" value="football"/>足球<br>
<input type="checkbox" name="sports" value="basketball"/>蓝球<br>
<input type="checkbox" name="sports" value="f1"/>F1<br>
<input type="checkbox" name="sports" value="f3"/>赛车<br>
<input type="checkbox" name="sports" value="f5"/>谈话<br>
<input type="checkbox" name="sports" value="qe"/>好<br>
<input type="checkbox" name="sports" value="fs"/>Helo<br>
<input type="checkbox" name="sports" value="a1"/>a1<br>
<input type="checkbox" name="sports" value="a2"/>a2<br>
<input type="checkbox" name="sports" value="a3"/>a3<br>
<input type="checkbox" name="sports" value="a4"/>a4<br>
<input type="checkbox" name="sports" value="a5"/>a5<br>
<input type="checkbox" name="sports" value="a6"/>a6<br>
<input type="checkbox" name="sports" value="a7"/>a7<br>
<br>
<p></p>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重置">
<INPUT title="选择所有的Banner,点击选定按钮" onclick="CheckAll(this.form)"
type="button" value="全选" name="chkall" style="cursor:hand">
<INPUT title="反向选择所有的Banner,点击选定按钮" onclick="CheckOthers(this.form)"
type="button" value="反选" name="chkOthers" style="cursor:hand">
</form>
</body>
</html>