简体   繁体   English

jQuery是否选中任何复选框

[英]jquery if any checkbox is checked

There are many questions regarding checked checkbox , but this is somewhat different. 关于选中复选框,有很多问题,但这有所不同。
I want to display a css property if any checkbox is checked by the user. 如果用户选中了任何复选框,我想显示css属性。 And if the user un-checks all the checkbox that css property should be removed. 并且,如果用户取消选中应删除css属性的所有复选框。
I found this code 我找到了这段代码

 if (jQuery('#frmTest input[type=checkbox]:checked').length > 0) { $(".css-check").css("background-color", "yellow"); } else { $(".css-check").css("background-color", "pink"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmTest"> <input type="checkbox" name="vehicle" value="Car" class="test">I have a car <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have a bike <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have nothing <br> </form> <div class="css-check">TEST</div> 

But this only works when i place checkbox="true" . 但这仅在我放置checkbox =“ true”时有效
In Short: 简而言之:
if no checkbox is checked by user , background-color should be pink. 如果用户未选中任何复选框,则background-color应该为粉红色。 And if even one checkbox is checked background-color should be yellow. 而且,即使选中了一个复选框,背景色也应该是黄色。

Try this: 尝试这个:

 jQuery('#frmTest input[type=checkbox]').on('change', function () { var len = jQuery('#frmTest input[type=checkbox]:checked').length; if (len > 0) { $(".css-check").css("background-color", "yellow"); } else if (len === 0) { $(".css-check").css("background-color", "pink"); } }).trigger('change'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmTest"> <input type="checkbox" name="vehicle" value="Car" class="test">I have a car <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have a bike <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have nothing <br> </form> <div class="css-check">TEST</div> 

You should be using $("#frmTest input[type='checkbox']").change(...) this event, like: 您应该使用$("#frmTest input[type='checkbox']").change(...)此事件,例如:

 $("#frmTest input[type='checkbox']").change(function(){ if (jQuery('#frmTest input[type=checkbox]:checked').length > 0) { $(".css-check").css("background-color", "yellow"); } else { $(".css-check").css("background-color", "pink"); } }).trigger("change"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmTest"> <input type="checkbox" name="vehicle" value="Car" class="test">I have a car <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have a bike <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have nothing <br> </form> <div class="css-check">TEST</div> 

If you want to add or remove CSS properties, the JQuery functions addClass and removeClass might come in handy like so: 如果要addremove CSS属性,则JQuery函数addClassremoveClass可能会派上用场,如下所示:

$("input[type=checkbox]").change(function () {
    if ($('#frmTest input[type=checkbox]:checked').length > 0) {
        $(".css-check").addClass("highlight");
    } else {
        $(".css-check").removeClass("highlight");
    }
});

CSS: CSS:

.highlight{
    background-color:red;
}

Here is the JSFiddle demo 这是JSFiddle演示

The code example that you provided missed the part where it waits for the checkbox changes, therefore you need to add the .change(...) event on the chekboxes: 您提供的代码示例错过了等待复选框更改的部分,因此您需要在.change(...)上添加.change(...)事件:

$("input[type=checkbox]").change(function () {...

The above code adds the css class highlight to the element identified by css-check class IF any checkbox is checked (that is checked element length is more than 0). 如果选中了任何复选框,则以上代码将css类highlight添加到css-check类所标识的元素中(即选中的元素长度大于0)。 Else it removes the class from it, hence, removing the CSS property. 否则,它从中删除该类,因此,删除了CSS属性。

You should bind the change event on the checkboxes because marking/unmarking triggers the change event and you can use .trigger(event) to trigger a specific event applied: 您应该在复选框上绑定change事件,因为marking/unmarking .trigger(event) marking/unmarking会触发更改事件,并且您可以使用.trigger(event)触发应用的特定事件:

 $('#frmTest :checkbox').change(function() { if (jQuery('#frmTest input[type=checkbox]:checked').length > 0) { $(".css-check").css("background-color", "yellow"); } else { $(".css-check").css("background-color", "pink"); } }).trigger('change'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmTest"> <input type="checkbox" name="vehicle" value="Car" class="test">I have a car <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have a bike <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have nothing <br> </form> <div class="css-check">TEST</div> 

Event this can be shorten like this: 事件可以这样缩短:

 $('#frmTest :checkbox').change(function() { $(".css-check").css("background-color", function(){ return jQuery('#frmTest input[type=checkbox]:checked').length > 0 ? "yellow" : "pink"; }); }).trigger('change'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmTest"> <input type="checkbox" name="vehicle" value="Car" class="test">I have a car <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have a bike <br> <input type="checkbox" name="vehicle" value="Car" class="test">I have nothing <br> </form> <div class="css-check">TEST</div> 

Please try this code: 请尝试以下代码:

<script type="text/javascript">
$(document).ready(function() {
if (jQuery('#frmTest input[type=checkbox]:checked').length > 0) {
    $(".css-check").css("background-color", "yellow");
} else {
    $(".css-check").css("background-color", "pink");
}
$("input[type=checkbox]").change(function () {
if (jQuery('#frmTest input[type=checkbox]:checked').length > 0) {
    $(".css-check").css("background-color", "yellow");
} else {
    $(".css-check").css("background-color", "pink");
}
}); 

}); 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM