//注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop
1、根据id获取checkbox
$("#checkbox1");
2、获取所有的checkbox
$("input[type='checkbox']");
$("input[name='cb']");
3、获取所有选中的checkbox
$("input:checkbox:checked");
$("input[type='checkbox']:checked");
$("input:[name='ck']:checked");
4、获取checkbox值
$("#id").val();
5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
$.each(function(index,element))是对数组,json和dom结构等的遍历
index - 选择器的 index 位置。
element - 当前的元素(也可使用 “this” 选择器)
6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr(“checked”))
if ($("#d1").prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
7、设置checkbox为选中状态
$("#d1").attr("checked",true);
$("#d1").attr("checked","checked");
8、设置checkbox为不选中状态
$("#d1").attr("checked",false);
$("#d1").removeAttr("checked","");
9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled",true);
$("input[type='checkbox']").attr("disabled","disabled");
$("input[type='checkbox']").prop("disabled", true);
$("input[type='checkbox']").prop("disabled", "disabled");
10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled",false);
$("input[type='checkbox']").removeAttr("disabled");
$("input[type='checkbox']").prop("disabled","");
$("input[type='checkbox']").prop("disabled",false);
官方建议:
具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()