gpt4 book ai didi

javascript - 为什么 '+' 运算符要连接我的数字?

转载 作者:行者123 更新时间:2023-11-28 09:07:29 24 4
gpt4 key购买 nike

我想访问在输入框中输入的值,然后获取它们的总和,但是 javascript 正在连接结果。这是代码

<script>

$(document).ready(function(){
$( "#dateSelector" ).click(function(){
$(this).datepicker();
});
});

$(document).ready(function(){
$("#ocb750cb").click(function(){
var closingBal = 0;
var openingBal = document.getElementById("ocb750ob").value;
alert(openingBal);
Number(openingBal);
var stockBal = document.getElementById("ocb750sb").value;
alert(stockBal);
Number(stockBal);
var sold = document.getElementById("ocb750sl").value;
alert(sold);
var store = document.getElementById("ocb750cb");
store.value = (stockBal + openingBal) - sold;
});



});



</script>



<th class = "main brand">OCB 750</th>
<th class = "main"><input type="text" maxlength="5" id="ocb750ob"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750sb"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750tl"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750sl"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750rs"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750cb"></th>

假设我输入 5 和 2,我的输出将是 52 而不是 7,它正在连接

最佳答案

您的值具有字符串格式,您需要使用 parceInt

关于javascript - 为什么 '+' 运算符要连接我的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16810224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com