javascript-dropdown onchange事件直到第二个实例才触发
作者:互联网
我遇到了最奇怪的问题,尽管我确定这很简单-我很困惑.我有一个事件,该事件根据下拉菜单隐藏和显示div.当我从菜单中选择某些东西时,它什么都不做.但是,如果我将其更改为第二个选择,则效果很好.有人知道我搞砸了吗?感谢您的协助.
function call1()
{
document.getElementById("quest1").onchange = function () {
alert("ON CHANGE FIRED");
var val = this.options[this.selectedIndex].value;
document.getElementById("area1z").style.display = (val == "0") ? "block" : "none";
document.getElementById("area1a").style.display = (val == "1") ? "block" : "none";
document.getElementById("area1b").style.display = (val == "2") ? "block" : "none";
document.getElementById("area1c").style.display = (val == "3") ? "block" : "none";
document.getElementById("area1d").style.display = (val == "4") ? "block" : "none";
};
}
<select id="quest1" onChange="call1()">
<option value=0></option>
<option value=1>Stretch (Maintaining a lifestyle choice for an amount of time) </option>
<option value=2>Sum (i.e. saving money, counting workouts, etc.</option>
<option value=3>Loss (i.e. Weightloss)</option>
<option value=4>None</option>
</select>
<div id=area1z style="display:none">Please Choose a Quest</div>
<div id=area1a style="display:none">Stretch</div>
<div id=area1b style="display:none">Sum</div>
<div id=area1c style="display:none">Loss</div>
<div id=area1d style="display:none">None</div>
解决方法:
从下面删除onChange():
<select id="quest1" onChange="call1()">
这样写:
document.getElementById("quest1").onchange = function () {
alert("ON CHANGE FIRED");
var val = this.options[this.selectedIndex].value;
document.getElementById("area1z").style.display = (val == "0") ? "block" : "none";
document.getElementById("area1a").style.display = (val == "1") ? "block" : "none";
document.getElementById("area1b").style.display = (val == "2") ? "block" : "none";
document.getElementById("area1c").style.display = (val == "3") ? "block" : "none";
document.getElementById("area1d").style.display = (val == "4") ? "block" : "none";
};
标签:onchange,html-select,html,javascript 来源: https://codeday.me/bug/20191030/1966764.html