2017-03-01 6 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 AflostabelPerMaand = '0' + zaaksoortMsnp.value; '! –

答えて

1

あなたは、あなたが使用する必要がありますchangeリスナー内で選択されたオプションの値を取得したい場合:this.options[this.selectedIndex].value

あなたはchangeリスナーを1回だけ適用する必要があります。最後の2行は不要です。

次のコードは動作するはずです:

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

最初に 'if'sは不要です! –

+0

はい、そうです。私は私のコメントを変更しました。 – kotapeter

+0

OPは私のものではなくこの答えを探しているようだ! –

1

変更あなたの機能をこの

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

"AflostabelPerMaandの01,02などの値はダミーテキストです。" – kotapeter

+0

次のコード – NielsSanders

関連する問題