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 POST
ed 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)
精彩评论