简体   繁体   English

选择和取消选择所有复选框

[英]Select and Unselect all checkboxes

I'm not able to implement these things,In this page I want to do implement 3 things: 我无法实现这些事情,在此页面中,我想实现以下三件事:

1> By default Apple and Cat will be checked in this page. 1> 默认情况下,将在此页面中选中Apple和Cat。 2> For Startall all the features will be checked and disabled. 2> 对于Startall,将检查并禁用所有功能。 3> For Stopall, all the features will be disabled as well as default (AC) value will be disabled.* 3> 对于Stopall,将禁用所有功能以及默认 (AC)值。*

 <script type="text/javascript"> //This jquery is using for on selecting the checkbox,it will assign the text field of policyName and features
    $(document).ready(function() { 
        $('.check').click(function(){
            $("#policyName").val('Start'); 
            $("#features").val('');
                    $(".check").each(function(){
                if($(this).prop('checked')){
                    $("#policyName").val($("#policyName").val() + $(this).val());   
                    $("#features").val($("#features").val() + $(this).data('name'));
                    }           
            });
         });
    });
    </script>

Here is my jsp page : 这是我的jsp页面:

<div align="center" id="checkboxes">
<input type="checkbox" name="startall" data-name="Startall" class="check" id="check" value="all"> All &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="stopall" data-name="Stopall" class="check" id="check" value="stopall"> STOPall &nbsp;&nbsp;
<input type="checkbox" name="apple" data-name="Apple" class="check" id="check" value="a"> Apple &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="ball" data-name="Ball" disabled="disabled" checked="checked"  id="check" value="b"> Ball 
    &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="cat" data-name="Cat" class="check" id="check" value="a"> Cat &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="dog" data-name="Dog" checked="checked" disabled="disabled"  id="check" value="d"> Dog 
    &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="elephant" data-name="Elephant" disabled="disabled" checked="checked"  id="check" value="e"> 
    Elephant &nbsp;&nbsp;&nbsp;
</div>

Any Assistance....will be apprciated. 任何协助...将不胜感激。

Try this 尝试这个

ID in your HTML should be unique .. Try using different id's or classes instead.. I have written the code using the name attribute which is a slow selector.. HTML中的ID应该是唯一的 ..尝试改用其他ID或类。.我已经使用name属性(这是一个缓慢的选择器)编写了代码。

$(document).ready(function() {
$('[name="apple"], [name="cat"]').prop('checked', true);

$('[name="startall"]').on('click', function() {
    var $checkboxes = $('input[type="checkbox"]').not('[name="startall"], [name="stopall"]');
    if (this.checked) {
        $checkboxes.prop({
            checked: true,
            disabled: false
        });
        $('#textbox').val( $(this).attr('data-name'));
    }
    else{
         $checkboxes.prop({
            checked: false
        });
         $('#textbox').val('');
    }
});

$('[name="stopall"]').on('click', function() {
    var $checkboxes = $('input[type="checkbox"]').not('[name="startall"], [name="stopall"]');
    if (this.checked) {
        $checkboxes.prop({
            checked: false,
            disabled: true
        });
        $('#textbox').val( $(this).attr('data-name'));
    }
    else{
         $checkboxes.prop({
            disabled: false
        });
        $('#textbox').val('');
    }
});

});​ });

UPDATED DEMO 更新的演示

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

相关问题 如何使用Java Swing取消选择和撤消复选框? - How to unselect and undo with checkboxes using Java Swing? 删除功能无法正常工作选中所有复选框 - delete functionality not working properly select all checkboxes 如何使用expandlistview选择所有复选框? - How to use select all checkboxes with expandedlistview? 选择特定父级的所有复选框 - Select all checkboxes from a specific parent 按钮Onclick在android中选择和取消选择状态 - Button Onclick Select and unselect state in android 如何选择所有复选框,同时单击android中的主复选框? - How to select all checkboxes, while clicking the main checkbox in android? 如何使按钮在recyclerView中选中所有复选框? - How do I make button select all checkboxes in my recyclerView? 我有三个复选框,因为连续三列都被选中,但是我想从行中选择一个。 如果选择一个则取消选择 - i have three checkbox as three columns in a row all are get selected but i want to select one from the row. If one is select other to are unselect 尝试使我的全选/清除所有按钮选中和取消选中复选框 - Trying to make my select all/clear all button check and uncheck checkboxes 遍历Swing中的所有复选框 - Iterate through all checkboxes in Swing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM