选择和取消选择所有复选框 [英] Select and Unselect all checkboxes

查看:76
本文介绍了选择和取消选择所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法实现这些事情,在此页面中,我想实现以下三件事:

I'm not able to implement these things,In this page I want to do implement 3 things:

1> 默认情况下,此页面将选中Apple和Cat. 2> 对于Startall,将检查并禁用所有功能. 3> 对于Stopall,将禁用所有功能以及默认的(AC)值.*

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>

这是我的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.

推荐答案

尝试一下

ID 应该是唯一的.尝试改用其他ID或类.

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

这篇关于选择和取消选择所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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