js获取复选框的值

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">




  <script>
function checkbox()
{
var str=document.getElementsByName("box");
var objarray=str.length;
var chestr="";
for (i=0;i<objarray;i++)
{//欢迎来到站长特效网,我们的网址是www.zzjs.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。
  if(str[i].checked == true)
  {
   chestr+=str[i].value+",";
  }
}//欢迎来到站长特效网,我们的网址是www.zzjs.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。
if(chestr == "")
{
  alert("请先选择一个爱好~!");
}
else
{
  alert("您先择的是:"+chestr);
}
}
</script>
<a href="<#ZC_BLOG_HOST#>">站长特效网</a>,站长必备的高质量网页特效和广告代码。zzjs.net,站长js特效。<hr>
<!--欢迎来到站长特效网,我们网站收集大量高质量js特效,提供许多广告代码下载,网址:www.zzjs.net,[email protected],用.net打造靓站-->
选择您的爱好:
  <input type="checkbox" name="box" id="box1" value="跳水" />跳水
  <input type="checkbox" name="box" id="box2" value="跑步" />跑步
  <input type="checkbox" name="box" id="box3" value="听音乐" />听音乐
  <input type="button" name="button" id="button" οnclick="checkbox()" value="提交" />
 </head>


 <body>


 function allcanl(){
var arr = document.getElementsByTagName("input");
var strs="";
for(var i=0;i<arr.length;i++){
  if(arr[i].type == "checkbox" && arr[i].checked){
       //做事情
     strs=strs+arr[i].value+","; 
   }
}
  var idcards=strs.substring(0,strs.length-1);
  window.location.href="/search/carsear4/myAttention.jsp?idcards="+idcards;
 }

  
 </body>
</html>

猜你喜欢

转载自blog.csdn.net/linwei_hello/article/details/10080693