今天介绍一下:
复选框全选、复选框反选、复选框取值等等
<div class="demo-kj"> <div class="demo-btn"> <button id="checkall">全选/反选</button>   <button id="submit">查看选中值</button> </div> <label><input type="checkbox" name="ids[]" class='ids' value="1"/> value==1</label> <label><input type="checkbox" name="ids[]" class='ids' value="2"/> value==2</label> <label><input type="checkbox" name="ids[]" class='ids' value="3"/> value==3</label> <label><input type="checkbox" name="ids[]" class='ids' value="4"/> value==4</label> <label><input type="checkbox" name="ids[]" class='ids' value="5"/> value==5</label> <label><input type="checkbox" name="ids[]" class='ids' value="6"/> value==6</label> <label><input type="checkbox" name="ids[]" class='ids' value="7"/> value==7</label> <label><input type="checkbox" name="ids[]" class='ids' value="8"/> value==8</label> <label><input type="checkbox" name="ids[]" class='ids' value="9"/> value==9</label> <label><input type="checkbox" name="ids[]" class='ids' value="10"/> value==10</label> <label><input type="checkbox" name="ids[]" class='ids' value="11"/> value==11</label> <label><input type="checkbox" name="ids[]" class='ids' value="12"/> value==12</label> <label><input type="checkbox" name="ids[]" class='ids' value="13"/> value==13</label> <label><input type="checkbox" name="ids[]" class='ids' value="14"/> value==14</label> <label><input type="checkbox" name="ids[]" class='ids' value="15"/> value==15</label> </div>
<script src="./jquery.js"></script>
<script>
/************ 全选两种写法 ***********/
$("#checkall").click(function(){
// 第一种写法
$("input[name='ids[]']").each(function(){
if(this.checked){
this.checked = false;
}else{
this.checked = true;
}
});
// 第二种写法
$('.ids').each(function () {
if ($(this).attr("checked")) {
$(this).removeAttr("checked");
} else {
$(this).attr("checked",'true');
}
})
})
/***********************************/
/***** 获取Checked值的两种写法 *****/
// 方法1:在后台接收到的值如:1,21,2,23,25
function getCheckedValue1(){
var ids = [];
$('.ids').each(function(){
if($(this)[0].checked)ids.push($(this).val());
});
return ids.join(',');
}
// 方法2:在后台接收到的值如: ["11","2","3"] 在后台要用json_decode一下转成数组
function getCheckedValue2(){
var id = new Array();
$("input[name='ids[]']").each(function(i,obj){
if(this.checked) id[i] = this.value;
});
return JSON.stringify(id);
}
/***********************************/
$("#submit").click(function(){
var ids = getCheckedValue1();
alert(ids);
})
</script>转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/159.html
1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意

