3

我遇到了最奇怪的问题,尽管我确信这很简单——我很难过。我有一个基于下拉菜单隐藏和显示 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>
4

3 回答 3

4

从以下内容中删除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";
};

例子

于 2013-10-21T08:46:42.260 回答
0

这是您正在尝试做的演示。

从代码中删除“onchaange”或“document.getElementById("quest1").onchange = function () {}”。

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";
  };

jsfiddle 链接:http: //jsfiddle.net/2M74g/1/

于 2013-10-21T10:09:37.863 回答
0
              <script>
                            function call1()
                        {

                          alert("ON CHANGE FIRED");
                          var val=document.getElementById("quest1").value;
                          alert(val);
                          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";

                        }
                        </script>
                        <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>
于 2013-10-21T08:46:02.300 回答