简体   繁体   中英

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. 2> For Startall all the features will be checked and disabled. 3> For Stopall, all the features will be disabled as well as default (AC) value will be disabled.*

 <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 :

<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..

$(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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