最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Summing variables with parseInt(); Not Working - Stack Overflow

programmeradmin0浏览0评论

What is the proper way to add the sum of multiple variables in Javascript?

This is what I'm trying to do. I've tried it with and without the quotes around my variables. I'm not getting a NaN or an Undefined or anything. No output whatsoever.

function setstat(){
document.getElementById('date').value = window.opener.document.getElementById('thisday').value;
document.getElementById('name').value = window.opener.document.getElementById('element_7').value;
document.getElementById('time').value = window.opener.document.getElementById('stwa').value;

inbcalls = window.opener.document.getElementById('element_6').value;
document.getElementById('totinb').value = inbcalls;
inbcallsp = parseInt("inbcalls",10);
asaptotal = window.opener.document.getElementById('asapcalls').value;
document.getElementById('asaptot').value = asaptotal;
asaptotalp = parseInt("asaptotal",10);
faxtotal = window.opener.document.getElementById('faxcalls').value;
document.getElementById('faxtot').value = faxtotal;
faxtotalp = parseInt("faxtotal",10);
obtotal = window.opener.document.getElementById('obcalls').value;
document.getElementById('obtot').value = obtotal;
totalcalls = inboundcallsp + asaptotalp + faxtotalp + obtotalp;
document.getElementById('totsum').value = totalcalls;
}

What is the proper way to add the sum of multiple variables in Javascript?

This is what I'm trying to do. I've tried it with and without the quotes around my variables. I'm not getting a NaN or an Undefined or anything. No output whatsoever.

function setstat(){
document.getElementById('date').value = window.opener.document.getElementById('thisday').value;
document.getElementById('name').value = window.opener.document.getElementById('element_7').value;
document.getElementById('time').value = window.opener.document.getElementById('stwa').value;

inbcalls = window.opener.document.getElementById('element_6').value;
document.getElementById('totinb').value = inbcalls;
inbcallsp = parseInt("inbcalls",10);
asaptotal = window.opener.document.getElementById('asapcalls').value;
document.getElementById('asaptot').value = asaptotal;
asaptotalp = parseInt("asaptotal",10);
faxtotal = window.opener.document.getElementById('faxcalls').value;
document.getElementById('faxtot').value = faxtotal;
faxtotalp = parseInt("faxtotal",10);
obtotal = window.opener.document.getElementById('obcalls').value;
document.getElementById('obtot').value = obtotal;
totalcalls = inboundcallsp + asaptotalp + faxtotalp + obtotalp;
document.getElementById('totsum').value = totalcalls;
}
Share Improve this question asked Jul 5, 2012 at 2:24 I wrestled a bear once.I wrestled a bear once. 23.4k20 gold badges72 silver badges122 bronze badges 4
  • Have you tried tracing each and every one of them in the console yet? – elclanrs Commented Jul 5, 2012 at 2:26
  • Well, "faxtotal" isn't an int, it's a string. Without knowing what you're actually doing, tough to help. What does the JS console day? – Dave Newton Commented Jul 5, 2012 at 2:28
  • Are the domains of the opener and the openee the same? Also, check your console for errors. – Ry- Commented Jul 5, 2012 at 2:28
  • 1 you know what, i'm retarted, ignore me. thanks anyway guys. i spend hours fixing the simplest things because i can't figure out how to use firebug. which failed to point out that "inboundcallsp" is undefined. – I wrestled a bear once. Commented Jul 5, 2012 at 2:44
Add a ment  | 

4 Answers 4

Reset to default 2

Why are you quoting the variable names?

inbcallsp = parseInt("inbcalls",10);

should be:

inbcallsp = parseInt(inbcalls, 10);

And the same for the rest of them. You want to parse the value of the variables, not the names of the variables; those will always result in NaN.

asaptotalp = parseInt("asaptotal",10); "asaptotal" is recognize as the string not the variable you should not quote it

When using parseInt always specify the radix as 10.

The function singnature: parseInt(string, radix)

The radix is optional but if ommited, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)

Example: parseIn("05") ==== 0 -> true

parseIn("05", 10) ==== 5 -> true

Don't use ParseInt, sometimes it will not return the proper value.

Better use Number, for example:

var i=Number(your value)
发布评论

评论列表(0)

  1. 暂无评论