Jquery gets the value of the checkbox selected
- 2020-03-30 02:28:18
- OfStack
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<mce:style><!--
--></mce:style><style mce_bogus="1">
</style>
<title>JS Gets the value of the check box selected </title>
</head>
<body>
<input type="checkbox" name="test" value="0" />0
<input type="checkbox" name="test" value="1" />1
<input type="checkbox" name="test" value="2" />2
<input type="checkbox" name="test" value="3" />3
<input type="checkbox" name="test" value="4" />4
<input type="checkbox" name="test" value="5" />5
<input type="checkbox" name="test" value="6" />6
<input type="checkbox" name="test" value="7" />7
<input type="button" onclick="chk()" value=" mention Hand in " />
</body>
</html
JS code
<mce:script src="jquery.js" mce_src="jquery.js"></mce:script><!-- This is loaded jquery.js File if not used jquery Can be removed -->
<mce:script type="text/javascript"><!--
function chk(){
var obj=document.getElementsByName('test'); //Select all objects whose name="'test'" and return the array
//Once we get the array of objects, we loop through to see if it's selected
var s='';
for(var i=0; i<obj.length; i++){
if(obj[i].checked) s+=obj[i].value+','; //If selected, add value to the variable s
}
//So now that we check the value of s we know what the check box is
alert(s==''?' You haven't chosen anything yet! ':s);
}
function jqchk(){ //Jquery gets the check box value
var chk_value =[];
$('input[name="test"]:checked').each(function(){
chk_value.push($(this).val());
});
alert(chk_value.length==0 ?' You haven't chosen anything yet! ':chk_value);
}
// --></mce:script>
A few other operations on the checkbox
1. The selection
Cancel all selections
3. Select all the odd Numbers
4. Deselect
5. Get all the selected values
Js code
$("document").ready(function(){
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');// Future generations
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//To cancel all
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//Select all odd Numbers
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){// The selected
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//Output the selected value
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+"/r/n";
//alert($(this).val());
})
alert(str);
})
})
HTML code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>louis-blog >> jQuery right checkbox The operation of the </title>
<mce:script type='text/javascript' src="http://leotheme.cn/wp-includes/js/jquery/jquery.js" mce_src="http://leotheme.cn/wp-includes/js/jquery/jquery.js"></mce:script>
<SCRIPT LANGUAGE="JavaScript">
<!--
$("document").ready(function(){
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');// Future generations
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//To cancel all
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//Select all odd Numbers
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){// The selected
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//Output the selected value
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+"/r/n";
//alert($(this).val());
})
alert(str);
})
})
-->
</SCRIPT>
</HEAD>
<body style="text-align:center;margin: 0 auto;font-size: 12px;" mce_style="text-align:center;margin: 0 auto;font-size: 12px;">
<div style="border: 1px solid #999; width: 500px; padding: 15px; background: #eee; margin-top: 150px;">
<form name="form1" method="post" action="">
<input type="button" id="btn1" value=" Future generations ">
<input type="button" id="btn2" value=" To cancel all ">
<input type="button" id="btn3" value=" Select all odd Numbers ">
<input type="button" id="btn4" value=" The selected ">
<input type="button" id="btn5" value=" Gets all the selected values ">
<br /><br />
<input type="checkbox" name="checkbox" value="checkbox1">
checkbox1
<input type="checkbox" name="checkbox" value="checkbox2">
checkbox2
<input type="checkbox" name="checkbox" value="checkbox3">
checkbox3
<input type="checkbox" name="checkbox" value="checkbox4">
checkbox4
<input type="checkbox" name="checkbox" value="checkbox5">
checkbox5
<input type="checkbox" name="checkbox" value="checkbox6">
checkbox6
</form>
</div>
</body>
</HTML>