简体   繁体   中英

dropdown onchange event not firing until second instance

I am having the strangest problem, although I'm sure it's something simple - I'm stumped. I have an event that hides and shows divs based on a dropdown menu. When I pick something from my menu it does absolutely nothing. However, if I change it to a second selection, it works perfectly. Anyone know what I screwed up? Thanks for any and all assistance.

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>

Remove onChange() from following:

<select id="quest1" onChange="call1()">

Write in this way:

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

Example

              <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>

Here is a Demo that you are trying to do.

Either you remove the "onchaange" or the "document.getElementById("quest1").onchange = function () {}" from the code.

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 link: http://jsfiddle.net/2M74g/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM