js获得checkbox选中值及input后面的文本

js如何获得多个checkbox选中的值及input后面所跟的文本

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>验证js获得多个checkbox选中值及input后面所跟文本</title>
   <script type="text/javascript">
       function clickBjjb() {
	   var checkboxValue= new Array();
	   var checkboxText= new Array();
	   var checkboxStr=document.getElementsByName("bjjb");  
	   for(var i=0; i<checkboxStr.length; i++){
		if(checkboxStr[i].checked){
	            //alert(checkboxStr[i].value+","+checkboxStr[i].nextSibling.nodeValue);
		    checkboxValue.push(checkboxStr[i].value);
                    checkboxText.push(checkboxStr[i].nextSibling.nodeValue);
		 }
	    } 
	    //输出值和文本
	     alert("checkboxValue:"+checkboxValue);
             alert("checkboxText:"+checkboxText);
           //把获得的数据转换为字符串传递到后台           
           checkboxValue=checkboxValue.toString();
         checkboxText=checkboxText.toString();
         window.location='某Action/netWorkingUpdate?checkboxValue='+checkboxValue+"&checkboxText="+checkboxText;

  } 
</script> 
</head> 
<body> 
   <form id="checkboxform" method="post" action="">
    <input type="checkbox" name="bjjb" value="1">交通事故<br>
    <input type="checkbox" name="bjjb" value="2">自然灾害<br>
    <input type="checkbox" name="bjjb" value="3">恶劣天气<br>
   <input type="checkbox" name="bjjb" value="4">严重违法行为<br>
   <input type="checkbox" name="bjjb" value="5">路面损毁<br>
  <input type="button" οnclick="clickBjjb()" value="报警级别" />
 </form> 
</body>
</html>







 

后台获得参数为:

//获得的均为数组值。

String checboxValues=request.getParameter("checboxValue");

String checboxTexts=request.getParameter("checboxText");

//得到每个具体值

String checboxValue=checboxValues.split(",");

String checboxText=checboxTexts.split(",");

备注:

1:nextSibling是获得当前对象的下一个对象,nodeValue是返回一个节点的值

2:使用该方法必须保证文本值在input后面,否则checkboxStr[i].nextSibling.nodeValue获取不到文本值


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值