我目前正在使用此功能,当您按下复选框时,下拉菜单 (textbox_1) 将被禁用。我的问题是,如何修改此代码以通过相同的点击也禁用 textbox_2 ......
<html>
<head>
<script type="text/javascript">
// setup a bit of code to run after the document has loaded. (note that its set on window)
window.addEventListener('load', function(){
potential_checkboxes = document.getElementsByTagName('input');
for(i = 0; i < potential_checkboxes.length; i ++) {
element = potential_checkboxes[i];
// see if we have a checkbox
if (element.getAttribute('type') == 'checkbox') {
// initial setup
textbox = document.getElementById(element.getAttribute('rel'));
textbox.disabled = element.checked;
// add event handler to checkbox
element.addEventListener('change', function() {
// inside here, this refers to the checkbox that just got changed
textbox = document.getElementById(this.getAttribute('rel'));
// set disabled property of textbox to not checked property of this checkbox
textbox.disabled = this.checked;
}, false);
}
}
}, false);
</script>
</head>
<body>
<h1>Enable/disable input based on checkbox.</h1>
<form>
<label for="textbox_1">
<SELECT class="enteredYear" id="textbox_1" name=year>
<OPTION selected value="">Year</OPTION>
<OPTION value=2013>2014</OPTION>
<OPTION value=2013>2013</OPTION>
<OPTION value=2012>2012</OPTION>
</SELECT>
</label>
<label for="textbox_2">
Textbox 1:
<input id="textbox_2" type="text" value="some value" />
</label>
<br />
<input id=="checkbox_1" type="checkbox" rel="textbox_1"/>
<label for="checkbox_1">I have a classic car.</label>
<hr />
<form>
</body>
</html>