gpt4 book ai didi

javascript - 为什么我的整数加起来像字符串

转载 作者:行者123 更新时间:2023-11-30 11:24:52 26 4
gpt4 key购买 nike

我正在尝试创建一个程序,在给定特定值时为椭圆创建方程。当我尝试添加两个变量时,整数像字符串一样添加。前任。 -4 + 4 应该等于 0 但最终等于 -44

var vertexY = 0
var vertex2Y = 0
function solveEllipse(){
vertexY = document.getElementById("ellipseVertexYInput").value
vertex2Y = document.getElementById("ellipseVertexY2Input").value
centerY = (vertexY + vertex2Y)
console.log(centerY)
}
<form method="post" class="forms" id="ellipseVertexDiv">
Vertex
<br/>
Y
<input type="text" name="vertexY" class="ellipseVariableInput" id="ellipseVertexYInput">
</form>
<form method="post" class="forms" id="ellipseVertex2Div">
Vertex 2
<br/>
Y
<input type="text" name="vertexY2" class="ellipseVariableInput" id="ellipseVertexY2Input">
<input type="button" name="submitButton" value="Submit" class="forms submitButton" id="ellipseSubmit" onclick="solveEllipse()">
</form>

最佳答案

因为 document.getElementById("ellipseVertexYInput").value 返回一个字符串。

您可以使用 + 将其转换为整数:+vertexY

var vertexY = 0
var vertex2Y = 0
function solveEllipse(){
vertexY = document.getElementById("ellipseVertexYInput").value;
vertex2Y = document.getElementById("ellipseVertexY2Input").value;
centerY = +vertexY + +vertex2Y;
console.log(centerY);
}
<form method="post" class="forms" id="ellipseVertexDiv">
Vertex
<br />
Y
<input type="text" name="vertexY" class="ellipseVariableInput" id="ellipseVertexYInput">
</form>
<form method="post" class="forms" id="ellipseVertex2Div">
Vertex 2
<br/>
Y
<input type="text" name="vertexY2" class="ellipseVariableInput" id="ellipseVertexY2Input">
<input type="button" name="submitButton" value="Submit" class="forms submitButton" id="ellipseSubmit" onclick="solveEllipse()">
</form>

关于javascript - 为什么我的整数加起来像字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393648/

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