簡體   English   中英

選擇選項值並將值分配給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);

如果要在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"> 

改變你的功能

function () {
    var AflostabelPerMaand = '0' + this.value;
    document.getElementById("fldMinimaleAfloswaardePerMaand").value = AflostabelPerMaand;
}

暫無
暫無

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

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