admin管理员组

文章数量:1415099

I have been trying to figure out the solution to my problem for a few hours now. I'm still fairly new to Javascript, so please forgive me if I sound stupid. Sadly I don't know jQuery and I'm starting to think that it's inhibiting me to e up with a solution.

What I am trying to do is have a check box that enables disabled radio buttons. If that makes sense.

Example:

  • Choice 1
  • sub choice
  • sub choice
  • sub choice

I would like to be able to click the first checkbox (Choice 1) to enable the sub choices. If it's not clicked, the sub choices should be disabled. :-)

Here is what I've got so far:

<script>
function enableRadios(x) {
    var formElement = eval("checkbox" + x)
    if (formElement[0].disabled) {
        formElement[0].disabled = false
        formElement[1].disabled = false
    } else {
        formElement[0].disabled = true
        formElement[1].disabled = true
    }
 }</script>

<body><input type="checkbox" name="main" value="main" onClick="enableRadios(x)">
<input disabled type="radio" value="1" name="x">
<input disabled type="radio" value="2" name="x">
<input disabled type="radio" value="3" name="x">
<input disabled type="radio" value="4" name="x">
<input disabled type="radio" value="5" name="x">
<input disabled type="radio" value="6" name="x"></body>

I really appreciate your help!

I have been trying to figure out the solution to my problem for a few hours now. I'm still fairly new to Javascript, so please forgive me if I sound stupid. Sadly I don't know jQuery and I'm starting to think that it's inhibiting me to e up with a solution.

What I am trying to do is have a check box that enables disabled radio buttons. If that makes sense.

Example:

  • Choice 1
  • sub choice
  • sub choice
  • sub choice

I would like to be able to click the first checkbox (Choice 1) to enable the sub choices. If it's not clicked, the sub choices should be disabled. :-)

Here is what I've got so far:

<script>
function enableRadios(x) {
    var formElement = eval("checkbox" + x)
    if (formElement[0].disabled) {
        formElement[0].disabled = false
        formElement[1].disabled = false
    } else {
        formElement[0].disabled = true
        formElement[1].disabled = true
    }
 }</script>

<body><input type="checkbox" name="main" value="main" onClick="enableRadios(x)">
<input disabled type="radio" value="1" name="x">
<input disabled type="radio" value="2" name="x">
<input disabled type="radio" value="3" name="x">
<input disabled type="radio" value="4" name="x">
<input disabled type="radio" value="5" name="x">
<input disabled type="radio" value="6" name="x"></body>

I really appreciate your help!

Share Improve this question edited Apr 23, 2013 at 7:37 Sadfsa Asffds asked Apr 23, 2013 at 7:32 Sadfsa AsffdsSadfsa Asffds 311 gold badge1 silver badge3 bronze badges 1
  • Hi there, great question, Javascript is not my thing so all I can do is refer you to this similar question that maybe you could inherit into your work? My assumption would be this part is wrong, but again I don't use javascript, "if (formElement[0].disabled) { formElement[0].disabled = false formElement[1].disabled = false } else { formElement[0].disabled = true formElement[1].disabled = true }" stackoverflow./questions/5215048/… – Philip Gullick Commented Apr 23, 2013 at 7:42
Add a ment  | 

1 Answer 1

Reset to default 2

You need to pass a string containing the radio name 'x', not pass a variable x:

<script>
function toggleRadios(name) {
    var elems = document.getElementsByName(name);
    for(i=0; i<elems.length; i++) {
        elems[i].disabled = !elems[i].disabled;
    }
}
</script>

<input type="checkbox" name="main" value="main" onclick="toggleRadios('x')">
<input disabled type="radio" value="1" name="x">
<input disabled type="radio" value="2" name="x">
<input disabled type="radio" value="3" name="x">
<input disabled type="radio" value="4" name="x">
<input disabled type="radio" value="5" name="x">
<input disabled type="radio" value="6" name="x">

http://jsfiddle/samliew/B6tF7/

本文标签: formsEnabling and disabling checkboxes with javascriptStack Overflow