使用JQuery获取被选中的checkbox的value值
2016-12-01 20:45
501 查看
上网查了一下,感觉一些人回答得真的是不知所云,要么代码不够简便。或者是有些想装逼成分等。
以下为使用JQuery获取input checkbox被选中的值代码:
<html>
<head>
<meta charset="gbk">
<!-- 引入JQuery -->
<script src="jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<input type="checkbox" value="橘子" name="check">橘子1</input>
<input type="checkbox" value="香蕉" name="check">香蕉1</input>
<input type="checkbox" value="西瓜" name="check">西瓜1</input>
<input type="checkbox" value="芒果" name="check">芒果1</input>
<input type="checkbox" value="葡萄" name="check">葡萄1</input>
<input type="button" value="方法1" id="b1">
<input type="button" value="方法2" id="b2">
</body>
<script>
//方法1
$("#b1").click(function(){
//$('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked')
//意思是选择被选中的checkbox
$.each($('input:checkbox:checked'),function(){
window.alert("你选了:"+
$('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
});
});
//方法2
$("#b2").click(function(){
$.each($('input:checkbox'),function(){
if(this.checked){
window.alert("你选了:"+
$('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
}
});
});
</script>
</html>
以下为使用JQuery获取input checkbox被选中的值代码:
<html>
<head>
<meta charset="gbk">
<!-- 引入JQuery -->
<script src="jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<input type="checkbox" value="橘子" name="check">橘子1</input>
<input type="checkbox" value="香蕉" name="check">香蕉1</input>
<input type="checkbox" value="西瓜" name="check">西瓜1</input>
<input type="checkbox" value="芒果" name="check">芒果1</input>
<input type="checkbox" value="葡萄" name="check">葡萄1</input>
<input type="button" value="方法1" id="b1">
<input type="button" value="方法2" id="b2">
</body>
<script>
//方法1
$("#b1").click(function(){
//$('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked')
//意思是选择被选中的checkbox
$.each($('input:checkbox:checked'),function(){
window.alert("你选了:"+
$('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
});
});
//方法2
$("#b2").click(function(){
$.each($('input:checkbox'),function(){
if(this.checked){
window.alert("你选了:"+
$('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
}
});
});
</script>
</html>
相关文章推荐
- 使用JQuery获取被选中的checkbox的value值 以及全选、反选
- 使用 Jquery 选中获取 checkbox radio 和 select的 value 和 text
- 使用JQuery获取被选中的checkbox的value值
- 使用Linq绑定三级treeview 并获取被选中的checkbox所对应的text值
- jquery中如何获取同一组radio中被选中的value
- [DOM元素处理] 使用jQuery获取iframe元素的value属性值 IE-firefox可以用
- 使用 jquery 获取一组或者单个 checkbox 的选中状态的值
- jquery获取checkbox的value
- asp.net绑定checkboxlist--------jquery绑定click事件,获取checkbox的value和显示值
- 关于jquery版本为1.6+使用checkbox的checked属性来判断一个checkbox是否被选中问题
- Jquery遍历checkbox获取选中项value值的方法
- jQuery获取form中Select/textarea/checkbox/radio选择的Text和Value等其他常见操作
- Jquery 获取Asp.Net CheckBoxList的Value,Text
- 使用 jquery 获取一组或者单个 checkbox 的选中状态的id
- JQuery获取被选中复选框checkbox的个数
- jquery中如何获取同一组radio中被选中的value
- 如何使用Jquery获取被选中的radio值
- jQuery获取Radio,CheckBox选择的Value值
- asp.net checkboxlist 后台动态绑定,前台jquery获取value
- jQuery获取Radio,CheckBox选择的Value值