簡體   English   中英

dropdown onchange事件直到第二個實例才開始

[英]dropdown onchange event not firing until second instance

我遇到了最奇怪的問題,雖然我確信它很簡單 - 我很難過。 我有一個事件隱藏並顯示基於下拉菜單的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";
};

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

這是您正在嘗試的演示。

要么從代碼中刪除“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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM