选择选项值并为var(javascript)赋值(select option value and assigning value to var (javascript))

编程入门 行业动态 更新时间:2024-10-19 04:22:58
选择选项值并为var(javascript)赋值(select option value and assigning value to var (javascript))

编辑:我已经按如下方式更改了代码。 现在没有任何代码可以工作。 (即使我的代码确实有效,我也没有与你分享。)

function berekeningAflostabel() { var zaaksoortMsnp; var AflostabelPerMaand; document.getElementById("fldZaakSoortMsnp").addEventListener("change", function () { AflostabelPerMaand = this.value; if(AflostabelPerMaand == 1) { zaaksoortMsnp = "Tekst1"; } if(AflostabelPerMaand == 2) { zaaksoortMsnp = "Tekst2"; } if(AflostabelPerMaand == 3) { zaaksoortMsnp = "Tekst3"; } if(AflostabelPerMaand == 4) { zaaksoortMsnp = "Tekst4"; } if(AflostabelPerMaand == 5) { zaaksoortMsnp = "Tekst5"; } if(AflostabelPerMaand == 6) { zaaksoortMsnp = "Tekst6"; } document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp; } } var eventZaaksoortMsnp = document.getElementById("fldZaakSoortMsnp"); eventZaaksoortMsnp.addEventListener("change", berekeningAflostabel);

Edit: I've changed the code as follows. Now none of the code seems to work. (Even my the code that did work and I didn't share with you.)

function berekeningAflostabel() { var zaaksoortMsnp; var AflostabelPerMaand; document.getElementById("fldZaakSoortMsnp").addEventListener("change", function () { AflostabelPerMaand = this.value; if(AflostabelPerMaand == 1) { zaaksoortMsnp = "Tekst1"; } if(AflostabelPerMaand == 2) { zaaksoortMsnp = "Tekst2"; } if(AflostabelPerMaand == 3) { zaaksoortMsnp = "Tekst3"; } if(AflostabelPerMaand == 4) { zaaksoortMsnp = "Tekst4"; } if(AflostabelPerMaand == 5) { zaaksoortMsnp = "Tekst5"; } if(AflostabelPerMaand == 6) { zaaksoortMsnp = "Tekst6"; } document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp; } } var eventZaaksoortMsnp = document.getElementById("fldZaakSoortMsnp"); eventZaaksoortMsnp.addEventListener("change", berekeningAflostabel);

最满意答案

如果您想在change侦听器中获取所选选项的值,那么您应该使用: this.options[this.selectedIndex].value

您只需要应用一次change侦听器。 最后两行是不必要的。

以下代码应该工作:

function berekeningAflostabel() {
  var zaaksoortMsnp;
  var AflostabelPerMaand;

  document.getElementById("fldZaakSoortMsnp").addEventListener("change", function() {
    AflostabelPerMaand = this.options[this.selectedIndex].value;

    if(AflostabelPerMaand == 1) {
      zaaksoortMsnp = "Tekst1";
    }
    if(AflostabelPerMaand == 2) {
      zaaksoortMsnp = "Tekst2";
    }
    if(AflostabelPerMaand == 3) {
      zaaksoortMsnp = "Tekst3";
    }

    document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp;
  });
}

berekeningAflostabel(); 
  
<select id="fldZaakSoortMsnp">
  <option value="1">teszt</option>
  <option value="2">teszt</option>
  <option value="3">teszt</option>
</select>

<input type="text" id="fldMinimaleAfloswaardePerMaand"> 
  
 

If you would like to get the selected option's value inside the change listener then you should use: this.options[this.selectedIndex].value

You need to apply the change listener only once. The last two line are unnecessary.

The following code should work:

function berekeningAflostabel() {
  var zaaksoortMsnp;
  var AflostabelPerMaand;

  document.getElementById("fldZaakSoortMsnp").addEventListener("change", function() {
    AflostabelPerMaand = this.options[this.selectedIndex].value;

    if(AflostabelPerMaand == 1) {
      zaaksoortMsnp = "Tekst1";
    }
    if(AflostabelPerMaand == 2) {
      zaaksoortMsnp = "Tekst2";
    }
    if(AflostabelPerMaand == 3) {
      zaaksoortMsnp = "Tekst3";
    }

    document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp;
  });
}

berekeningAflostabel(); 
  
<select id="fldZaakSoortMsnp">
  <option value="1">teszt</option>
  <option value="2">teszt</option>
  <option value="3">teszt</option>
</select>

<input type="text" id="fldMinimaleAfloswaardePerMaand"> 
  
 

更多推荐

本文发布于:2023-07-16 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:赋值   并为   选项   var   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!