简体   繁体   English

为什么我的 if 条件不适用于 javascript 中的 select 框?

[英]Why my if condition isn't working with select box in javascript?

In my code, I am trying to make a code in which I want to get value of select box and add value accordingly but my code is getting a bunch of errors.在我的代码中,我正在尝试编写一个代码,我想在其中获取 select 框的值并相应地添加值,但是我的代码出现了一堆错误。 Can anyone please help me.谁能帮帮我吗。 In code i just want to add 1 to selectbox value.在代码中,我只想将 1 添加到选择框值。 Eg: If i pressed on S and add then 1 should be added to S Code:例如:如果我按下 S 并添加,则应将 1 添加到 S 代码:

 function myFunction() { var x = document.getElementById("issue").value; var is1 = 0; var is2 = 0; var is3 = 0; if (x == document.getElementById("issue").value) { is1 = 1; } else if (x == document.getElementById("issue").value) { is2 = 1; } else if (x == document.getElementById("issue").value) { is3 = 1; } }
 Enter issue code: <select name="issue" id="issue"> <option value="i1">S</option> <option value="i2">W</option> <option value="i3">T</option> </select> <button onclick="myFunction()">Add</button>

You can simply create three variables and update their count based on the selected option.您可以简单地创建三个变量并根据所选选项更新它们的计数。

 let is1 = 0, is2 = 0, is3 = 0 function myFunction() { var x = document.getElementById("issue").value; if (x === "i1") is1++ else if (x === "i2") is2++ else if (x === "i3") is3++ document.getElementById("results").innerText=`Option S: ${is1}\nOption W: ${is2}\nOption T: ${is3}` }
 Enter issue code: <select name="issue" id="issue"> <option value="i1">S</option> <option value="i2">W</option> <option value="i3">T</option> </select> <button onclick="myFunction()">Add</button> <p id="results"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM