如果选择的国家/地区是美国,我想创建状态下拉列表,如果没有,则只需填写文本空间。我怎么能用HTML做到这一点?
答案 0 :(得分:1)
您必须在html中实现javaScript才能执行此操作。最简单的方法是隐藏/取消隐藏文本框和下拉菜单,具体取决于首选框中的选定项目。
HTML:
<!--Your Source Drop down-->
<select name="select1" id="select1">
<option value="Country1">Country 1</option>
<option value="US">United States</option>
</select>
<select name="select2" id="select2">
<!--List of States Here-->
</select>
<!--The Textbox (hidden)-->
<input type="text" id="case" name="myText" style="display:none">
在JavaScript上:
<script type="text/javascript">
var select1 = document.getElementById("select1");
var select2 = document.getElementById("select2");
var text1 = document.getElementById("case");
select1.onchange = function() {
//Show States, Hide Textbox (US Selected)
if (select1.selectedIndex == 1) {
select2.style.display='block';
text1.style.display='none';
}
else //Hide States, Show Textbox (Other Country is Selected)
{
select2.style.display='none';
text1.style.display='block';
}
}
</script>