无限精彩,DreamflyIT世界!

有一种执著叫坚持,有一种梦想叫超越!

导航

javascript怎么可以判断单选复选按钮是否选中,且传值

Posted on 2009-06-04 09:44  韦忠波  阅读(902)  评论(0编辑  收藏  举报

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
<script type="text/javascript">
function saveRadio(){
    var arr= document.getElementsByName("radio1");
    var v="";
    for(var i=0;i<arr.length;i++){
        if(arr[i].checked) {v = arr[i].value;break;}
    }
    alert(v);
}
function saveCheck(){
    var arr= document.getElementsByName("checkbox1");
    var v="";
    for(var i=0;i<arr.length;i++){
        if(arr[i].checked) v += arr[i].value + ",";
    }
    if(v!="") v = v.substr(0,v.length-1);
    alert(v);
}
</script>
</head>
<body>
<div>
01<input name="radio1" type="radio" value="01" />
02<input name="radio1" type="radio" value="02" />
03<input name="radio1" type="radio" value="03" />
</div>
<div>
<input type="button" value="save" onclick="saveRadio()" />
</div>
<div>
01<input type="checkbox" name="checkbox1" value="01" />
02<input type="checkbox" name="checkbox1" value="02" />
03<input type="checkbox" name="checkbox1" value="03" />
04<input type="checkbox" name="checkbox1" value="04" />
</div>
<div>
<input type="button" value="save" onclick="saveCheck()" />
</div>
</body>
</html>

 注意方法: getElementsByName,arr[i].checked,v.substr