【发布时间】:2010-02-08 13:27:41
【问题描述】:
我正在尝试根据在其他下拉列表中所做的选择将选项加载到下拉列表。我编写的代码几乎可以在所有主流浏览器、FF、Opera、Safari 上运行,但在 IE7 中无法运行。
这是我的 Javascript 代码:
<script type = "text/javascript">
var txt1 = "<option>state1<\/option><option>state2<\/option><option>state3<\/option><option>state4<\/option>";
var txt2 = "<option>stateA<\/option><option>stateB<\/option><option>stateC<\/option><option>stateD<\/option><option>stateE<\/option>";
function states_val() {
if(document.getElementById("country").options[0].selected==true) {
document.getElementById("states").disabled=true;
document.getElementById("states").innerHTML="<option>Select country<\/option>";
}
else if(document.getElementById("country").options[1].selected==true) {
document.getElementById("states").disabled=false;
document.getElementById("states").innerHTML=txt1;
}
else if(document.getElementById("country").options[2].selected==true) {
document.getElementById("states").disabled=false;
document.getElementById("states").innerHTML=txt2;
}
}
</script>
还有 HTML 代码:
<select id="country" name="name_country" onchange="states_val()">
<option>select</option>
<option>country1</option>
<option>country2</option>
</select>
<select id="states" name="name_states"></select>
我绑定了客户端脚本,并且只能使用 Javascript 来模拟它。请帮我调试代码。
【问题讨论】:
-
快速浏览一下,我看不出脚本有什么问题。可能是您期望立即触发 onchange 事件,而实际上它仅在控件失去焦点时触发(您单击/选择下拉框以外的任何内容)。跨度>
-
@miguel,对不起,我没明白你的意思。
-
@miguel,我的要求正如我所提到的,需要根据另一个选择元素更改选项值。
标签: javascript html events drop-down-menu