使用jquery更改复选框状态 [英] Using jquery to change checkbox status

查看:347
本文介绍了使用jquery更改复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目列表,其中有与其相关的复选框,这些项目在列表头下面,它也有一个复选框(仅用于数据库),实际上是从视图隐藏的。我需要这样做,如果选中标题下方的复选框,那么复选框也被勾选。但是我还需要这样做,如果所有复选框未选中,那么头一个是未选中...这将是多么困难?

I have a list of items that have checkboxes associated with them, and the items are beneath a list header, which also has a checkbox(for db purposes only) which is actually hidden from view. I need to make it so that if any of the checkboxes beneath the header is checked, then the header checkbox is checked as well. BUT I also need to make it so that if all of the checkboxes are unchecked, then the header one is unchecked...how hard is this going to be?

这是我现在的设置:

if($('.childCheckBox').is(":checked")){
$('#parentBox').attr('checked', true);
}

现在我想如果我做一些事情, if子句:

Now I am thinking that if I were to do something where I add an else if clause:

else if($('.proBox2').is(not)(":checked"){
alert("uncheck parent");
}

知道正确的语法is-not,或不是,或者甚至有什么,会做到这一点。有什么想法吗?我需要检查一个框,如果一个被选中,如果没有被选中,取消选中。 !!

But I don't know the correct syntax for is-not, or isn't, or even if there is anything that will do that. Are there any thoughts out there? I need the box to check if one is selected and uncheck if none are selected. Thanks!!

推荐答案

$('.proBox2').is(':not(:checked)')

此外,我不太确定第一个。 > 可以工作,但我可能会这样做,如果它不:

Also, I'm not so sure about the first one. It may work, but I'd probably do it like this if it doesn't:

$('.childCheckBox:checked').length > 0

这篇关于使用jquery更改复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆