如何通过复选框启用无线电组?

时间:2012-11-22 04:06:00

标签: javascript jquery html checkbox

我做了搜索,但我仍然无法修复它。请帮忙:

这是我的js代码:

<script type="text/javascript">
    $(document).ready(function() {
        $("#email_acc").click(function() {
            if ( $(this).is(":checked") ) {
                $("#email_group").attr("enabled","enabled");
            }
        });  

        $("#system_acc").click(function() {
            if ( $(this).is(":checked") ) {
                $("#system_group").attr("enabled","enabled");
            }
        }); 
    });
</script>

以下是我的HTML代码:

<input type="checkbox" name="email_acc" id="email_acc" />Email Account
<input type="checkbox" name="sys_acc" id="sys_acc" />System Account

<input type="radio" name="radio_email" value="create" class="email_group" id="radio_email_0" disabled="disabled"/>New
<input type="radio" name="radio_email" value="change" id="radio_email_1" disabled="disabled"/>Change
<input type="radio" name="radio_email" value="terminate" id="radio_email_2" disabled="disabled"\ />Termination

<input type="radio" name="radio_system" value="create" class="system_group" id="radio_system_0" disabled="disabled"/>New
<input type="radio" name="radio_system" value="change" id="radio_system_1" disabled="disabled" />Change
<input type="radio" name="radio_system" value="terminate" id="radio_system_2" disabled="disabled" />Termination

我不知道问题是什么。它只是不起作用。

4 个答案:

答案 0 :(得分:4)

$(document).ready(function() 
{

    $("#email_acc").click(function()
    { 
        if ( $(this).is(":checked") ) 
        {  
            $("input[name='radio_email']").removeAttr("disabled");
           //      ^----------------enable all the checkbox with common name
        }
    });  

    $("#sys_acc").click(function()
   //     ^---------corrected id
    {
        if ( $(this).is(":checked") )
        {   $("input[name='radio_system']").removeAttr("disabled");
       //         ^----------------enable all the checkbox with common name
        }
    }); 
});​

DEMO

DEMO 2

针对OP的新要求更新了代码,通过nnnnnn

清理了代码

更新 DEMO 3

答案 1 :(得分:0)

$("#email_acc").click(function()
    {
        if ( $(this).is(":checked") )
        {
            $("#email_group").attr("disabled","false");
                  or
            $("#email_group").attr("disabled",false);
        }
    });  

答案 2 :(得分:0)

没有"enabled"属性或属性,只有"disabled"属性或属性,可以将其设置为false以启用相关元素。 (“属性”位于源html中,但是对“属性”进行了动态更改,将其设置为布尔值以禁用或禁用。)

除非使用旧版本的jQuery,否则应使用.prop() method更新此属性。要启用整组单选按钮,您必须按name属性选择它们(或者为它们指定一个公共class并按其选择)。

$("#email_acc").click(function() { 
    if ( this.checked ) {  
        $('input[name="radio_email"]').prop('disabled', false);
    }
});

// and the same for the other checkbox and group.

请注意,您的第二个复选框有id="sys_acc",但您的JS正在尝试选择"#system_acc" - 您需要确保它们匹配。

如果取消选中该复选框,则需要再次禁用该组,然后您可以执行以下操作,删除if语句并将disabled属性设置为checked的倒数属性:

$('#email_acc').click(function() { 
    $('input[name="radio_email"]').prop('disabled', !this.checked);
});

// and again the same idea for the other checkbox and group.

演示:http://jsfiddle.net/UqTB3/

请注意,在这两种情况下,我都使用this.checked而不是$(this).is(":checked"):我发现前者更容易阅读,并且直接检查DOM元素的checked属性更有效而不是创建一个jQuery对象并使用.is(":checked")。但如果您真的热衷于使用jQuery来实现所有,那么您可以进行适当的替换。

答案 3 :(得分:0)

  1. 在输入html中添加名称
  2. 名称应该相同

    <script type="text/javascript">
       document.getElementById('cek3').onchange = function () {
           document.getElementById('fasilitas_kost1').disabled = !this.checked;
           document.getElementById('fasilitas_kost2').disabled = !this.checked;
           document.getElementById('fasilitas_kost3').disabled = !this.checked;
           document.getElementById('fasilitas_kost4').disabled = !this.checked;
           document.getElementById('fasilitas_kost5').disabled = !this.checked;
           document.getElementById('fasilitas_kost6').disabled = !this.checked;
           document.getElementById('fasilitas_kost7').disabled = !this.checked;
       };
    </script>