开发者

HTML select tag activation and deactivation

I want to 开发者_开发技巧make html select tag only activated when a check box is checked,how can i??


Using Javascript:

<script type='text/javascript'>

    if(document.getElementById("myCheckbox").checked) { 
      document.getElementById("mySelect").disabled = "";
    } 
    else { 
      document.getElementById("mySelect").disabled = "disabled"; 
    }

</script>

When this code is called, it checks if box is checked and disables select as appropriate.

Or, with PHP/HTML:

<form action='' method='post'>
<select <?PHP if($_POST['myCheckbox']==1) { echo "disabled='disabled'"; } ?> >
 <option>blah</option>
</select>

<input type='checkbox' name='myCheckbox' value='1' />
</form>

When the form is submitted, this checks if box has been POSTed and disables select as appropriate.


function handler() {
    var checkbox = document.getElementById("myCheckbox");
    var select = document.getElementById("mySelect");

    select.disabled = !checkbox.checked;
}

I tested the disabled property: It can be set to either true / false or "disabled" / "". Both methods work in all browsers.

(I tested in Chrome, Firefox, Safari, Opera and IE9 beta)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜