您的位置:首页 > Web前端 > JavaScript

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

2017-07-25 16:11 453 查看
转自:http://blog.csdn.net/u014079773/article/details/51865596

<!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" onclick="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获取不到文本值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript