的Html
<p id="hospitalorientation"><label>Hospital Orientation:</label>
<div id='buttons'>
<label><input id="radio1" type="radio" name="hospital" /> Not Complete </label>
<label><input id="radio2" type="radio" name="hospital" /> Complete </label>
</div>
<div id="list1" style="display: none;">
<label>Month Complete:
<select>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</label>
</div>
的Javascript:
document.getElementById("radio1").onchange = function(){
if(this.checked == true){
document.getElementById("list1").style.visibility = "hidden";
document.getElementById("list1").style.display = "none";
}
};
document.getElementById("radio2").onchange = function(){
if(this.checked == true){
document.getElementById("list1").style.visibility = "visible";
document.getElementById("list1").style.display = "block";
}
};
工作實例:http://jsfiddle.net/aSxXA/
下面是一個更新,這是一個完整的HTML頁面,注意身體onload事件如何註冊事件處理,我認爲當您試圖在之前註冊文檔時,文檔可能尚未加載
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type=text/javascript>
function registerEventHandlers()
{
document.getElementById("radio1").onchange = function(){
hideDiv(this,"list1")
};
document.getElementById("radio2").onchange = function(){
showDiv(this,"list1")
};
document.getElementById("radio3").onchange = function(){
hideDiv(this,"list2")
};
document.getElementById("radio4").onchange = function(){
showDiv(this,"list2")
};
}
function showDiv(targetElement,toggleElementId){
if (targetElement.checked == true) {
document.getElementById(toggleElementId).style.visibility = "visible";
document.getElementById(toggleElementId).style.display = "block";
}
}
function hideDiv(targetElement,toggleElementId){
if (targetElement.checked == true) {
document.getElementById(toggleElementId).style.visibility = "hidden";
document.getElementById(toggleElementId).style.display = "none";
}
}
</script>
</head>
<body onload="registerEventHandlers();">
<p id="hospitalorientation">
<label>
Hospital Orientation:
</label>
<div id='buttons'>
<label>
<input id="radio1" type="radio" name="hospital" /> Not Complete
</label>
<label>
<input id="radio2" type="radio" name="hospital" /> Complete
</label>
</div>
<div id="list1" style="display: none;">
<label>
Month Complete:
<select>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</label>
</div>
<p id="ppd">
<label>
PPD:
</label>
<div id='buttons'>
<label>
<input id="radio3" type="radio" name="ppd" /> Not Complete
</label>
<label>
<input id="radio4" type="radio" name="ppd" /> Complete
</label>
</div>
<div id="list2" style="display: none;">
<label>
Month Complete:
<select>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</body>
</html>
爲什麼不從一個JavaScript框架開始,它可以讓你相對容易地做到這一點。 jQuery是最流行的JS框架之一:http://jquery.com/ –