正好项目里要用到,自己写了一下,记下来,以后可以用用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test checkbox</title>
<script language="javascript">
function checkall()
{
var objForm = document.ckform;
var objLen = objForm.length;
for (var elcount = 0; elcount < objLen; elcount++)
{
if (objForm.elements[elcount].type == "checkbox")
{
objForm.elements[elcount].checked = true;
}
}
}
function clearall()
{
var objForm = document.ckform;
var objLen = objForm.length;
for (var elcount = 0; elcount < objLen; elcount++)
{
if (objForm.elements[elcount].type == "checkbox")
{
objForm.elements[elcount].checked = false;
}
}
}
function checkop(opvalue){
var objForm = document.ckform;
var objLen = objForm.length;
for (var elcount = 0; elcount < objLen; elcount++)
{
if (objForm.elements[elcount].type == "checkbox")
{
if (objForm.elements[elcount].id.indexOf(opvalue)==0){
objForm.elements[elcount].checked = true;
}
else{
objForm.elements[elcount].checked = false;
}
}
}
}
function checkresult(){
var objForm = document.ckform;
var objLen = objForm.length;
var flag=true;
var checkvalue="";
for (var elcount = 0; elcount < objLen; elcount++)
{
if (objForm.elements[elcount].type == "checkbox")
{
if(objForm.elements[elcount].checked ==true){
flag=false;
if (checkvalue==""){
checkvalue=objForm.elements[elcount].value;
}
else{
checkvalue=checkvalue+","+objForm.elements[elcount].value;
}
}
}
}
if (flag==true){
alert("至少选择一个");
}
alert(checkvalue);
}
</script>
</head>
<body>
<form name="ckform" method="post" action="">
<table width="100%" border="1">
<tr>
<td><input type="checkbox" id="1011" name="checkbox" value="1"></td>
<td><input type="checkbox" id="1022" name="checkbox" value="2"></td>
<td><input type="checkbox" id="1033" name="checkbox" value="3"></td>
</tr>
<tr>
<td><input type="checkbox" id="2011" name="checkbox" value="4"></td>
<td><input type="checkbox" id="2022" name="checkbox" value="5"></td>
<td><input type="checkbox" id="2033" name="checkbox" value="6"></td>
</tr>
<tr>
<td><input type="checkbox" id="3011" name="checkbox" value="7"></td>
<td><input type="checkbox" id="3022" name="checkbox" value="8"></td>
<td><input type="checkbox" id="3033" name="checkbox" value="9"></td>
</tr>
<tr>
<td><input type="button" name="bu1" id="bu1" value="全选" onClick="checkall();"><input type="button" name="bu2" id="bu2" value="全部不选" onClick="clearall();"></td>
<td><input type="button" name="Submit" value="选中一行" onClick="checkop('20');"></td>
<td><input type="button" name="Submit" value="提交" onClick="checkresult();"></td>
</tr>
</table>
</form>
</body>
</html>
posted on 2006-09-15 10:23
Java初心 阅读(1143)
评论(1) 编辑 收藏 所属分类:
Javascript/Css