代码之家  ›  专栏  ›  技术社区  ›  BoDiE2003

javascript:选中复选框时启用复选框列表(带原型)

  •  2
  • BoDiE2003  · 技术社区  · 15 年前

    我一直在使用jquery来完成这项工作,但是现在我需要使用原型来完成这项工作,并且由于缺乏文档,我很少感到困惑。

    我有两个复选框列表

    第一个列表:

    Check box 1
    Check box 2
    

    第二个列表:

    Check box x
    check box y
    check box z
    

    我需要使用原型的JS代码这样工作:第二个列表,除非我选中第一个列表中的一个复选框,否则将保持禁用状态。

    有什么建议吗?

    2 回复  |  直到 13 年前
        1
  •  4
  •   artlung    15 年前

    下面是javascript代码:

    Event.observe(window, 'load', function(){
        // for all items in the group_first class
        $$('.group_first').each(function(chk1){
            // watch for clicks
            chk1.observe('click', function(evt){
                // count how many of group_first
                // are checked, true if any are checked
                var doEnable = ($$('.group_first:checked').length > 0);
                // for each in group_second, enable the
                // checkbox, and remove the cssDisabled class
                $$('.group_second').each(function(item){
                    if (doEnable) {
                        item.enable().up('label').removeClassName('cssDisabled');
                    } else {
                        item.disable().up('label').addClassName('cssDisabled');
                    }
                });
            });
        });
    });
    

    基于此HTML:

    <fieldset>
        <legend>First Group</legend>
        <label><input type="checkbox" value="1"
            class="group_first" />Check box 1</label><br />
        <label><input type="checkbox" value="2"
            class="group_first" />Check box 2</label>
    </fieldset>
    
    
    <fieldset>
        <legend>Second Group</legend>
        <label class="cssDisabled"><input type="checkbox" value="x"
            class="group_second" disabled="disabled" />Check box x</label><br />
        <label class="cssDisabled"><input type="checkbox" value="y"
            class="group_second" disabled="disabled" />Check box y</label><br />
        <label class="cssDisabled"><input type="checkbox" value="z"
            class="group_second" disabled="disabled" />Check box z</label>
    </fieldset>
    

    添加此CSS:

    .cssDisabled { color: #ccc; }
    

    原型的文档非常好。以下是我使用的方法:

    对于那些对如何在jquery中实现这一点感兴趣的人:

    jQuery(document).ready(function(){ 
       $('.group_first').bind('click', function(){
            var doEnable = ($('.group_first:checked').length > 0);
            $('.group_second').each(function(){
                if (doEnable) {
                    $(this).attr('disabled', null);
                    $(this).parents('label').removeClass('cssDisabled');
                } else {
                    $(this).attr('disabled', 'disabled');
                    $(this).parents('label').addClass('cssDisabled');
                }
            });
        });
    });
    
        2
  •  0
  •   BoDiE2003    15 年前

    这是测试代码,我构建并检查第一个组复选框未启用组2复选框

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>toggle disabled</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="prototype.js" type="text/javascript"> 
    <script type="text/javascript">
    var checkboxes = $$('input[name="group1"]');
    checkboxes.each(function(s) {
        s.observe('click', function() {
            if(this.checked) {
                $$('input[name="group2"]').each(function(s) {
                    s.enable();
                });
            } else {
                if(!$$('input[name="group2"][checked="checked"]').length) {
                    alert('nothing checked');
                    $$('input[name="group2"]').each(function(s) {
                        s.disable();
                    });
                }
            }
        });
    });
    </script>
    
    </head>
    <body>
        <div>
            <label> Group 1 </label>
            <input type="checkbox" name="group1" value="test"/>
            <input type="checkbox" name="group1" value="test"/>
            <input type="checkbox" name="group1" value="test"/>
        </div>
        <div>
            <label> Group 2 </label>    
            <input type="checkbox" name="group2" disabled="disabled"/>
            <input type="checkbox" name="group2" disabled="disabled"/>
            <input type="checkbox" name="group2" disabled="disabled"/>
        </div>
    </body>
    </html>