关于checkbox的一些问题(全选,反选,以及取值)

时间:2024-04-16 19:27:34

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="../test.css">
<style>
*{
margin: 0;
padding: 0
}
tr ,td{
border:1px solid #332D2D;
}
td{
width: 50px;
}
.btn-group,#table{
margin-top:25px;
margin-left:25px;
}
button{
width: 100px;
height: 25px;
border-radius: 25px;
box-shadow: 2px 2px 1px #3F3A3A;
}

</style>
</head>
<body>
<div class="btn-group">
<button id="add" onclick="addRow()">添加行</button>
<button id="checkall" onclick="checkall()">全选</button>
<button id="checkinvert" onclick="inverse()">反选</button>
<button id="uncheckall" onclick="uncheckall()">全不选</button>
<button id="del" onclick="delcheck()">删除选中的</button>
<button id="sub" onclick="sub()">提交</button>
</div>
<table id="table" style="width:480px;">
<thead>
<th>one</th>
<th>two</th>
<th>three</th>
<th>four</th>
</thead>
</table>
</body>
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<script>
var count=0;
function addRow(){
$("#table").append('<tr><td><input type="checkbox" id="ch_'+count+ '" ></td>'
+'<td > <input type="text" id="na_'+count+ '" ></td>'
+'<td ><input type="text" id="age_'+count+ '" ></td>'
+'<td > <input type="text" id="ge_'+count+ '" ></td>'
+'</tr>');
count++;
}
function checkall(){
var a=$("input[id^='ch_']:checkbox");
for(var i=0;i<a.length;i++){
a[i].checked=true;
}

}
function uncheckall(){
var a=$("input[id^='ch_']:checkbox");
for(var i=0;i<a.length;i++){
a[i].checked=false;
}
}
function delcheck(){
var d=$("input[id^='ch_']");
d.each(function(){
if($(this).is(":checked")){
$(this).parent().parent().remove();}
})
}
function sub(){
var objs=[];
$("input[id^=ch_]:checkbox").each(function(){
if($(this).is(":checked")){
var id=$(this).attr("id");
var str=id.substring(3,4);
var obj=new Object();
obj.na=$("#na_"+str).val();
obj.age=$('#ge_'+str).val();
obj.ge=$('#age_'+str).val();
objs.push(obj);
}
})
}
function inverse(){
var a=$("input[id^=ch_]:checkbox");
a.each(function(){
if($(this).is(":checked")){
$(this).prop("checked",false);
}
else{
$(this).prop("checked",true);
}}
);
}
</script>
</html>