jQuery checkbox全选/取消全选实现代码
作者:bea
jQuery版本:1.3.2 代码如下: <html> <head> <script src="jquery-1.3.2.min.js" type="text/javascript"></script> </head> <body> <input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<b
jQuery版本:1.3.2
代码如下:
<html>
<head>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
<input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
<input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
<input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
<input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
<input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
<script type="text/javascript">
$("#chk_all").click(function(){
$("input[name='chk_list']").attr("checked",$(this).attr("checked"));
});
</script>
</body>
</html>
jQuery.attr 获取/设置对象的属性值,如:
代码如下:
$("input[name='chk_list']").attr("checked"); //读取所有name为'chk_list'对象的状态(是否选中)
$("input[name='chk_list']").attr("checked",true); //设置所有name为'chk_list'对象的checked为true
再如:
代码如下:
$("#img_1").attr("src","test.jpg"); //设置ID为img_1的<img>src的值为'test.jpg'
$("#img_1").attr("src"); //读取ID为img_1的<img>src值
下面的代码是获取上面实例中选中的checkbox的value值:
代码如下:
<script type="text/javascript">
//获取到所有name为'chk_list'并选中的checkbox(集合)
var arrChk=$("input[name='chk_list][checked]");
//遍历得到每个checkbox的value值
for (var i=0;i<arrChk.length;i++)
{
alert(arrChk[i].value);
}
</script>
哪位高手能把上面遍历的过程用$.each()写出来,不胜感激。
有用 | 无用
代码如下:
<html>
<head>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
<input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
<input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
<input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
<input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
<input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
<script type="text/javascript">
$("#chk_all").click(function(){
$("input[name='chk_list']").attr("checked",$(this).attr("checked"));
});
</script>
</body>
</html>
jQuery.attr 获取/设置对象的属性值,如:
代码如下:
$("input[name='chk_list']").attr("checked"); //读取所有name为'chk_list'对象的状态(是否选中)
$("input[name='chk_list']").attr("checked",true); //设置所有name为'chk_list'对象的checked为true
再如:
代码如下:
$("#img_1").attr("src","test.jpg"); //设置ID为img_1的<img>src的值为'test.jpg'
$("#img_1").attr("src"); //读取ID为img_1的<img>src值
下面的代码是获取上面实例中选中的checkbox的value值:
代码如下:
<script type="text/javascript">
//获取到所有name为'chk_list'并选中的checkbox(集合)
var arrChk=$("input[name='chk_list][checked]");
//遍历得到每个checkbox的value值
for (var i=0;i<arrChk.length;i++)
{
alert(arrChk[i].value);
}
</script>
哪位高手能把上面遍历的过程用$.each()写出来,不胜感激。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- ExtJS的FieldSet的column列布局
- 页面版文本框智能提示JS代码
- js url传值中文乱码之解决之道
- js trim函数 去空格函数与正则集锦
- div移动 输入框不能输入的问题
- ExtJS Window 最小化的一种方法
- JS获取dom 对象 ajax操作 读写cookie函数
- jquery中的$(document).ready()与window.onload的区别
- jQuery html()等方法介绍
- JQuery toggle使用分析
- JavaScript 读取图片实例代码
- javascript 随机抽奖程序代码
- javascript 图片放大效果函数
- JavaScript OOP类与继承
- JavaScript的eval JSON object问题
- Javascript 中文字符串处理额外注意事项
- Dojo 学习笔记入门篇 First Dojo Example
- flash 得到自身url参数的代码
- javascript 去字符串空格终极版(支持utf8)