jquery判断是否选中checkbox的方法:1、使用is判断,代码为【if($('#checkbox-id').is(':checked'))】;2、使用attr判断,代码为【if ($('#checkbox-id').attr()】。

推荐:《jquery视频教程》
本教程操作环境:windows7系统、jquery3.5.1版本,该方法适用于所有品牌电脑。
jquery判断是否选中checkbox的方法:
方法一(建议):
if ($("#checkbox-id").get(0).checked) {
// do something
}感谢豆瓣绿补充:
if ($("#checkbox-id")[0].checked) {
// do something
}方法二(建议):
if($('#checkbox-id').is(':checked')) {
// do something
}方法三(可能无效):
if ($('#checkbox-id').attr('checked')) {
// do something
}方法四:
if ($('#checkbox-id').prop('checked')) {
// do something
}jquery对checkbox的各种操作
//注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop
//1、根据id获取checkbox
$("#cbCheckbox1");
//2、获取所有的checkbox
$("input[type='checkbox']");//or
$("input[name='cb']");
//3、获取所有选中的checkbox
$("input:checkbox:checked");//or
$("input:[type='checkbox']:checked");//or
$("input[type='checkbox']:checked");//or
$("input:[name='ck']:checked");
//4、获取checkbox值
//用.val()即可,比如:
$("#cbCheckbox1").val();
//5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
//6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr("checked"))
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
});
//7、设置checkbox为选中状态
$('input:checkbox').attr("checked", 'checked');//or
$('input:checkbox').attr("checked", true);
//8、设置checkbox为不选中状态
$('input:checkbox').attr("checked", '');//or
$('input:checkbox').attr("checked", false);
//9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled", "disabled");//or
$("input[type='checkbox']").attr("disabled", true);//or
$("input[type='checkbox']").prop("disabled", true);//or
$("input[type='checkbox']").prop("disabled", "disabled");
//10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").removeAttr("disabled");//or
$("input[type='checkbox']").attr("disabled", false);//or
$("input[type='checkbox']").prop("disabled", "");//or
$("input[type='checkbox']").prop("disabled", false);相关免费学习推荐:JavaScript(视频)
以上就是jquery如何判断是否选中checkbox的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号