Javascript:选中复选框时启用复选框列表(使用原型)

时间:2010-05-06 15:32:46

标签: javascript html checkbox prototypejs

我一直在使用jquery来做这件事,但是现在我需要用Prototype做这件事而我很少因为缺乏文档而感到困惑

我有两个复选框列表

第一份清单:

Check box 1
Check box 2

第二个清单:

Check box x
check box y
check box z

我需要JS代码,使用原型来这样工作:第二个列表,除非我选中First List的一个复选框,否则仍然处于禁用状态。

有什么建议吗?

2 个答案:

答案 0 :(得分:4)

这是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; }

Prototype的文档非常好。这是我正在使用的方法:

对于那些对如何在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');
            }
        });
    });
});

答案 1 :(得分:0)

这是我构建的测试代码,并且检查第一组复选框是否未启用第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>
相关问题