选择选项值并为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">
更多推荐
发布评论