答案 0 :(得分:1)
我看到的主要问题是onchange
处理程序应该绑定到<select>
元素而不是<option>
元素。
似乎还有nested <form>
,这可能会导致问题。
function displayCountry(answer) {
document.getElementById(answer).style.display = "block";
if (answer == "China") {
document.getElementById("India").style.display = "none";
document.getElementById("USA").style.display = "none";
} else if (answer == "India") {
document.getElementById("China").style.display = "none";
document.getElementById("USA").style.display = "none";
} else if (answer == "USA") {
document.getElementById("China").style.display = "none";
document.getElementById("India").style.display = "none";
}
}
function displayProvince(answer) {
document.getElementById(answer).style.display = "block";
if (answer == "Beijing Municipality") {
document.getElementById("Tianjin Municipality").style.display = "none";
} else if (answer == "Tianjin Municipality") {
document.getElementById("Beijing Municipality").style.display = "none";
}
}
function displayChinaCity(answer) {
document.getElementById(answer).style.display = "block";
if (answer == "Beijing") {
document.getElementById("Dongcheng").style.display = "none";
} else if (answer == "Dongcheng") {
document.getElementById("Beijing").style.display = "none";
}
}
&#13;
<div class="container">
<h3>Add Client</h3>
<div class="tab-content">
<form action="/add/clients" method="post">
<div class="top-row">
<div class="field-wrap">
<label>Client ID<span class="req">*</span><input></label>
</div>
</div>
<div class="top-row">
<div class="field-wrap">
<label>Client name<span class="req">*</span><input></label>
</div>
</div>
<div class="field-wrap">
<label>Client type<span class= "req">*</span><select></select></label>
</div>
<div class="field-wrap">
<label>Client Origin<span class="req">*</span>
<select name="country" onchange="displayCountry(this.value)">
<option selected= "--">--</option>
<option value= "China" >China</option>
<option value= "India" >India</option>
<option value= "USA" >USA</option>
</select>
</label>
<div id="USA" style="display:none;">
<select></select>
</div>
<div id="China" style="display:none;"><br/>
Select Province<span class="req">*</span>
<select name="province" onchange="displayProvince(this.value)">
<option selected= "--">--</option>
<option value= "Beijing Municipality" >Beijing Municipality</option>
<option value= "Tianjin Municipality">Tianjin Municipality</option>
</select>
</div>
<div id="India" style="display:none;">
<select></select>
</div>
<div id="Beijing Municipality" style="display:none;"><br/>
Select City<span class="req">*</span>
<select name="city" onchange="displayChinaCity(this.value)">
<option selected= "--">--</option>
<option value= "Beijing">Beijing</option>
<option value= "Dongcheng">Dongcheng</option>
</select>
</div>
</div>
</form>
</div>
</div>
&#13;