下面的代码不是返回加法结果,而是将变量的值附加到数字上。 pl。帮助。提前致谢。
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="number" id="wd" value=1000>
<br>
Enter Height of chart
<input type="number" id="ht" value=1000>
<br>
<body>
<script type="text/javascript">
var retWidth = document.getElementById('wd').value;
var retHeight = document.getElementById('ht').value;
wd1 = retWidth + 5; // hoping to get result 1005
ht1 = retHeight + 5;
console.log("wd1 = "+wd1)
console.log("ht1 = "+ht1)
</script>
</body>
</html>
console log:
wd1 = 10005
ht1 = 10005
最佳答案
使用parseInt()方法将字符串转回数字。现在它们是字符串,因此添加两个字符串会将它们连接起来,这就是您得到 10005 的原因。 使用parseInt(宽度)+5
关于Javascript 添加输入值结果将它们连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36400570/