admin管理员组文章数量:1404626
I have an HTML div, which I use JavaScript to retrieve the value from, as below:
var oCreditStatus = document.getElementById('creditStatusField');
var oOrigValue = oCreditStatus.value;
var oOrigValue will always be a number, and sometimes 0 (Zero).
I now want to increment or decrement the number by 1. However, when I do the following:
var oNewValue = oCreditStatus.value + 1;
I get the value 01, then 011, which is wrong as I want 1,2.
I know it is not recognizing it as a number, but how do I get it to do so.
I have an HTML div, which I use JavaScript to retrieve the value from, as below:
var oCreditStatus = document.getElementById('creditStatusField');
var oOrigValue = oCreditStatus.value;
var oOrigValue will always be a number, and sometimes 0 (Zero).
I now want to increment or decrement the number by 1. However, when I do the following:
var oNewValue = oCreditStatus.value + 1;
I get the value 01, then 011, which is wrong as I want 1,2.
I know it is not recognizing it as a number, but how do I get it to do so.
Share Improve this question asked Jan 19, 2015 at 15:03 AlexAlex 4,01810 gold badges51 silver badges100 bronze badges 5-
4
oCreditStatus
's value is stored as a string. You will need to convert it to a number before adding with it:var oOrigValu = Number(oCreditStatus.value)
– ndugger Commented Jan 19, 2015 at 15:04 -
1
You may also use:
var oNewValue = +oCreditStatus.value + 1;
– Bhojendra Rauniyar Commented Jan 19, 2015 at 15:07 - possible duplicate of How to make a document.getElementById value into an integer variable, not a string? – James Thorpe Commented Jan 19, 2015 at 15:10
- There is absolutely nothing wrong with this question and no reason to mark it down. Anyone who ever voted down, do you wish to explain your reasons? @JamesThorpe ? – Alex Commented Jan 19, 2015 at 15:18
- @Alex I didn't downvote it - that was someone else. Just pointed out the potential duplicate. – James Thorpe Commented Jan 19, 2015 at 15:19
5 Answers
Reset to default 5You can't. Javascript engine will read it as a text/string. You've to use following code:
Edit. Use a radix (base 10) Thanks to @James for revision.
var oCreditStatus = document.getElementById('creditStatusField');
var oOrigValue = parseInt(oCreditStatus.value, 10); // use parseFloat for decimal.
var oOrigValue will always be a number, and sometimes 0 (Zero).
Just pass it to Number
function since oOrigValue
is always a number. (Note: 0
is also a number)
var oOrigValue = Number(oCreditStatus.value);
There are several other ways of doing the same by prefixing it with +
and ~~
In fact oOrigValue
is not a number, it's a string. To be able to add another number to it, you'll have to parse it first. There is several methods to do it:
var parsedValue = parseInt(oOrigValue, 10); // remember about the second parameter - the radix
or simpler, but less readable
var parsedValue = +oOrigValue;
try to this
var oCreditStatus = document.getElementById('creditStatusField').value*1;
You need to parse oCreditStatus.value
as an int
var oNewValue = parseInt(oCreditStatus.value) + 1;
本文标签: JavaScript var not being recognized as a numberStack Overflow
版权声明:本文标题:JavaScript var not being recognized as a number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744838158a2627758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论