如何通过复选框启用单选按钮组? [英] How to enable radio group by checkbox?

查看:106
本文介绍了如何通过复选框启用单选按钮组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了搜索,但我仍然无法解决它。请帮助:

I did search all over but i still cant fix it. Please help:

这是我的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代码:

The following shows my html code:

<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

我不知道是什么问题,只是不工作。

I donno what is the problem.It just doesn't work.

推荐答案

没有启用属性或属性,只是 code>属性或属性,可以设置为 false 以启用相关元素。 (属性在您的源html中,但动态更改为属性,将其设置为布尔值以禁用或不禁用。)

There is no "enabled" attribute or property, just a "disabled" attribute or property which can be set to false to enable the element(s) in question. (The "attribute" is in your source html, but dynamic changes are made to the "property", setting it to a boolean value to disable or not.)

使用旧版本的jQuery,您应该使用 .prop()方法更新此属性。要启用整个单选按钮组,你必须通过 name 属性来选择它们(或者给他们一个公共的

Unless using an old version of jQuery, you should use the .prop() method to update this property. To enable the whole group of radio buttons you have to select them all by their name attribute (or give them a common class and select by that).

$("#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选择 - 你需要确保它们匹配。

Note that your second checkbox has an id="sys_acc" but your JS is trying to select with "#system_acc" - you need to make sure they match.

如果您需要重新禁用该组,如果取消选中该复选框,那么您可以执行以下操作:删除如果语句并设置 属性的 c>属性:

If you need to disable the group again if the checkbox is unchecked then you can do the following, removing the if statement and setting the disabled property to the inverse of the checked property:

$('#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元素的检查属性直接比创建一个jQuery对象并使用 .is(:checked)。但是如果你真的热衷于使用jQuery来处理一切,你可以进行适当的替换。

Note that in both cases I've used this.checked instead of $(this).is(":checked"): I find the former easier to read, and it is more efficient to check the DOM element's checked property directly than to create a jQuery object and use .is(":checked"). But if you're really keen on using jQuery for everything you can make the appropriate substitution.

这篇关于如何通过复选框启用单选按钮组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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