一、js获取checkbox被选中的值?
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>javascript获取复选框值方法</title>
</head>
<body>
<p>
<input type="checkbox" name="hobbies" id="hobbies" class="hobbies" value="1">游泳
<input type="checkbox" name="hobbies" id="hobbies" class="hobbies" value="2">跑步
</p>
<p>
<button id="btn1">获取复选框值</button>
</p>
</body>
</html>
方法1---$(“input[name=’xxxx’]:checked”) 被选中的复选框对象集合
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
$('#btn1').click(function () {
$("input[name='hobbies']:checked").each(function () {
alert($(this).val());
});
});
</script>
</script>
方法2--- var ck_taskIds = []
$("input[type='checkbox']:checked").each(function(){
ck_taskIds.push($(this).val());
});
var pp = ck_taskIds.join(",");
结果是:[7,8,2,1,6,5],
js中push可以将新元素添加到数组中,数组可以是 空数组,但是数组不能为null
var number;
var my_array = new Array();
my_array.push (5, 6, 7);
my_array.push (8, 9);
结果集为:5,6,7,8,9
js中pop将数组中最后一个元素移除,并返回移除的元素
var number;
var my_array = new Array();
my_array.push (5, 6, 7);
my_array.push (8, 9);
number = my_array.pop();
while (number != undefined)
{
document.write (number + " ");
number = my_array.pop();
}
二、
页面和后台数据传输
1,将对象或者数据封装给一个变量传输。
var pp = ck_taskIds.join(",");
myJson(hcrwkk_list.baseAction + hcrwkk_list.method.manualallocat,{"storeId":pp.toString(),"storeName":pp2},true,function(data){
var vv = date...
2,
var params = {"branchCode":branchCode,"cpicid":cpicid};
myJson(yhjssz.baseAction + "resetPassword",params,true,function(data){
var vv = data...
}
3,传输对象
<form id="gkfpgz_updateQuery" action="web/renewalMessage/update"><form>
var params = $('#gkfpgz_updateQuery').serialize();
myJson(gkfpgz.baseAction + gkfpgz.method.update,params,true,function(data){var vv =data...}