我可以't在javascript中添加两个变量.如果我更改要添加的变量的顺序,它要么忽略第二个变量,要么连接

I can't add two variables in javascript. It either ignores the second variable or it concatenates if I change the order of the variables to be added.

本文关键字:变量 添加 连接 顺序 第二个 如果 javascript 我可以 两个      更新时间:2023-09-26

我不能在javascript中添加两个变量。如果我更改要添加的变量的顺序,它要么忽略第二个变量,要么连接。

var total = zbv[0] + zval + zter;

total = total.toFixed(2);
document.getElementById("ztotalvalue").innerHTML = total;
var nfa = Number(googForm.nfa.value);
alert(total);
alert(nfa)
var equity = nfa + total;
alert(equity);

.toFixed()返回字符串,可以

var equity = nfa + +total // +total will cast string to number

你的问题措辞不好。其次,如果某个东西被连接在一起,那么这一定意味着它是一个字符串(这样就可以回答你的问题)。您需要转换为integer并清理代码。要调试代码,请使用console.log,打开元素检查器并转到"console"。