0 html javascript variables function drop-down-menu
我的html中有一个下拉列表,其中的值显示在这里,
<select id="thedropdown">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
而我的javascript:
function myFunction()
{
var a = document.getElementById("thedropdown");
var b = a.options[a.selectedIndex].value;
var y=b;
var x=y+2;
var demoP=document.getElementById("demo")
demoP.innerHTML= x;
}
Run Code Online (Sandbox Code Playgroud)
然而,当我点击按钮时的答案是22它应该是4.你可以看到我的问题.提前帮助干杯
现在你的代码连接字符串.你需要parseInt()和parseFloat()(或Number()或者+)增加前的值.
使用时parseInt()确保始终指定第二个参数(基数):
b = parseInt(a.options[a.selectedIndex].value, 10);
Run Code Online (Sandbox Code Playgroud)