2017-03-01 43 views
1

編輯:我已經更改了代碼,如下所示。現在沒有任何代碼似乎工作。 (即使我是做的工作,我沒有與你分享代碼)選擇選項值並將值賦給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); 
+1

提示:'=''VS =='。 –

+0

您沒有在您提供的代碼中調用該函數! –

+0

作爲優化,只需將值設置爲'0x'(x是數字)或至少刪除所有'if'並執行此操作:var var AflostabelPerMaand ='0'+ zaaksoortMsnp.value;'! –

回答

1

如果你想獲得所選選項的的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">

+1

「如果」是不必要的! –

+0

是的,你說得對。我修改了我的評論。 – kotapeter

+0

看來,OP正在尋找這個答案不是我的! –

1

改變你的函數這個

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

」AflostabelPerMaand的01,02等值是一個虛擬文本。「 – kotapeter

+0

以下代碼 – NielsSanders