简体   繁体   中英

javascript to disable/enable checkbox and dropdown menu

Hi I am trying to disable a drop down when a checkbox is checked and the when the checkbox is unchecked enable the drop down. below is my javascript and it is working when I check the checkbox but it won't let me uncheck the checkbox:

     <script type="text/javascript">

             function  test(obj){

                     if (document.getElementsByName("aForm[0].info")[0].checked = true) {  
                             alert("here");
                             document.getElementsByName("aForm[0].gender")[0].disabled = true; 
                    }
                     else document.getElementsByName("aForm[0].info")[0].checked = false;{
                     document.getElementsByName("aForm[0].gender")[0].enabled = true;


             }
             }

             </script>

and here is my jsp

   <logic:iterate id="data" name="aForm" property="testList">
            <tr>

                <td>
                <html:checkbox indexed="true" name="aForm" styleId="something" property="info" onclick="test(this)"/>

                </td>
                  <td>
                   <html:select name="aForm" indexed="true" property="unit" styleId="dropdown">
                                    <html:optionsCollection name="aForm" property="selectGender" value="value" label="label"/>
                            </html:select>
                            >
                  </td>
            </tr>
        </logic:iterate>

You are storing a value, not comparing

The if statement

if( document.getElementsByName("aForm[0].info")[0].checked = true )

should be

if( document.getElementsByName("aForm[0].info")[0].checked == true )

notice the ==

And what is with the else?

else document.getElementsByName("aForm[0].info")[0].checked = false;{

You are treating it like an else if(), else does not have a statement there.

Your code should throw JavaScript errors when run.

And there is no such thing as enabled, it is just disabled, true/false.

Your code should look like

function test(obj) {
    if (document.getElementsByName("aForm[0].info")[0].checked = true) {
        document.getElementsByName("aForm[0].gender")[0].disabled = true;
    }
    else {
        document.getElementsByName("aForm[0].gender")[0].disabled= false;
    }
}

and it can be simplified down to just 3 lines:

function test(obj) {
        document.getElementsByName("aForm[0].gender")[0].disabled = document.getElementsByName("aForm[0].info")[0].checked;    
}

and to make it work without the element names:

<script>
    function test(cb){
        cb.parentNode.parentNode.getElementsByTagName("select")[0].disabled = cb.checked;
    }
</script>

<table>
  <tbody>
    <tr>
        <td><input type="checkbox" onclick="test(this);"></td>
        <td><select><option>a</option><option>b</option></select></td>
    </tr>
    <tr>
        <td><input type="checkbox" onclick="test(this);"></td>
        <td><select><option>a</option><option>b</option></select></td>
    </tr>   
  </tbody>
</table>

Note: parentNode.parentNode is bound to break if the page structure changes. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM