jquery获取复选框被选中的值的方法

2019-03-2209:51:50WEB前端开发Comments2,286 views字数 3528阅读模式

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

jquery获取复选框被选中的值的方法,需要的朋友可以参考下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

复制代码 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<mce:style><!--文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

--></mce:style><style mce_bogus="1">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

</style>
<title>JS获取复选框被选中的值</title>
</head>
<body>
<input type="checkbox" value="0" />0
<input type="checkbox" value="1" />1
<input type="checkbox" value="2" />2
<input type="checkbox" value="3" />3
<input type="checkbox" value="4" />4
<input type="checkbox" value="5" />5
<input type="checkbox" value="6" />6
<input type="checkbox" value="7" />7
<input type="button" value="提 交" />
</body>
</html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

JS代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

复制代码 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

<mce:script src="jquery.js" mce_src="jquery.js"></mce:script><!--这是载入jquery.js文件,如果不使用jquery可以去掉-->
<mce:script type="text/javascript"><!--文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

function chk(){
var obj=document.getElementsByName('test'); //选择所有name="'test'"的对象,返回数组
//取到对象数组后,我们来循环检测它是不是被选中
var s='';
for(var i=0; i<obj.length; i++){
if(obj[i].checked) s+=obj[i].value+','; //如果选中,将value添加到变量s中
}
//那么现在来检测s的值就知道选中的复选框的值了
alert(s==''?'你还没有选择任何内容!':s);
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

function jqchk(){ //jquery获取复选框值
var chk_value =[];
$('input[name="test"]:checked').each(function(){
chk_value.push($(this).val());
});
alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);
}
// --></mce:script>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

对checkbox的其他几个操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

1. 全选
2. 取消全选
3. 选中所有奇数
4. 反选
5. 获得选中的所有值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

js代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

复制代码 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

$("document").ready(function(){
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');//全选
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){//反选
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//输出选中的值
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+"/r/n";
//alert($(this).val());
})
alert(str);
})
})文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

html代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

复制代码 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

<!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 对checkbox的操作</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');//全选
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){//反选
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//输出选中的值
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+"/r/n";
//alert($(this).val());
})
alert(str);
})
})
-->
</SCRIPT>
</HEAD>
<body mce_style="text-align:center;margin: 0 auto;font-size: 12px;">
<div>
<form method="post" action="">
<input type="button" value="全选">
<input type="button" value="取消全选">
<input type="button" value="选中所有奇数">
<input type="button" value="反选">
<input type="button" value="获得选中的所有值">
<br /><br />
<input type="checkbox" value="checkbox1">
checkbox1
<input type="checkbox" value="checkbox2">
checkbox2
<input type="checkbox" value="checkbox3">
checkbox3
<input type="checkbox" value="checkbox4">
checkbox4
<input type="checkbox" value="checkbox5">
checkbox5
<input type="checkbox" value="checkbox6">
checkbox6
</form>
</div>
</body>
</HTML>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10235.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/10235.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定