日期:2014-05-18  浏览次数:20760 次

html checkbox 与JS的问题…
部分相关代码如下:
selCol()为一JS函数。

<html>
<input   type= "checkbox "   name= "chk "   value= "a "   onClick= "selCol(XXX) ">
<input   type= "checkbox "   name= "chk "   value= "b "   onClick= "selCol(XXX) ">
<input   type= "checkbox "   name= "chk "   value= "c "   onClick= "selCol(XXX) ">
</html>
  如何使代码中XXX=当前被点checkbox   的选中状态(true,false)?
  因为有多个checkbox   name是一样的!有没有类似的this.checked或者me.checked   的呢,谢谢!

------解决方案--------------------
<script language= "javascript ">
function del()//用于判断记录有没有选中的函数
{
var flag=true;//用于存储有没有选择的变量,false为有选,true没选
var temp= " ";//复选框的值,并事先给它预赋值为空,否则它的值为未定义
var tmp;//复选框数
if((document.form1.answer.length)== "undefined ") //判断复选框的数目,当只有1个复选框时不能使用复选框length属性,所以它的值为undefined
{
tmp=1
}
else
{
tmp=document.form1.answer.length
}
//接下来就分两种情况来处理,一种是只有1个复选框,另一种是多个复选框
if(tmp==1)
{
if(document.form1.answer.checked) //当复选框被选中时
{
flag=false;
temp=document.form1.answer.value //因为只有1个复选框,所以将值直接付给temp
}
}
else //另外一种情况有多个复选框时
{
for(i=0;i <document.form1.answer.length;i++) //for循环
{
if(document.form1.answer[i].checked) //当answer[i]被选中时为true
{
if(temp== " ")
{
flag=false;
temp=document.form1.answer[i].value
}
else
{
flag=false;
temp=temp+ ", "+document.form1.answer[i].value
}
}
}
}
if(flag)
{
alert( "对不起,你还没选择! ");
}
else
{
//name=document.form1.name.value
//alert(name)
if(confirm( "确实要删除? "))
{
//window.location= "delnews.asp?id= "+temp;//发送id号到删除处理页
alert( "删除ID为 "+temp);
}
}
//return !flag;
}

//全选函数
function checkall(all)//用于判断全选记录的函数
{
var a=document.getElementsByName( "answer ");//返回answer的集合
for(var i=0; i <a.length;i++)a[i].checked=all.check1.checked;
}

</script>
<form name= "form1 " method= "post " action= " ">
<input type= "checkbox " name= "answer " value= "1 "> 1 <br>
<input type= "checkbox " name= "answer " value= "2 "> 2 <br>
<input type= "checkbox " name= "answer " value= "3 "> 3 <br>
<input type= "checkbox " name= "answer " value= "4 "> 4 <br> 全选
<input type= "checkbox " name= "check1 " onclick= "checkall(form1) "> <br>
<input type= "button " name= "b1 " value= "删除 " onClick= "del() ">
</form>


------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function selCol(c)
{
if(c.checked == true)
{
alert(c.value+ " is checked ");
}
else
{
alert(c.value+ " is uncheck ");
}
}