admin管理员组文章数量:1403348
I am trying to do something I though would be really simple. add the value of 1 to a number.
However the number was plain text and things are getting a little confusing. As you may be able to guess, I am fairly new to this.
My goal is to add 1 point to an existing score, the score has one decimal place, eg 1.3. So the desired result after adding a point is 2.3. However The current script I wrote is returning adds a point to the second decimal place and I don't understand why.
Thanks for the help.
var getPoints = parseInt($('.awesome-points').text(), 10).toFixed(1);
alert(getPoints);
var addPoint = 1;
var newScore = getPoints + addPoint;
$('.awesome-points').text(newScore);
Mark
I am trying to do something I though would be really simple. add the value of 1 to a number.
However the number was plain text and things are getting a little confusing. As you may be able to guess, I am fairly new to this.
My goal is to add 1 point to an existing score, the score has one decimal place, eg 1.3. So the desired result after adding a point is 2.3. However The current script I wrote is returning adds a point to the second decimal place and I don't understand why.
Thanks for the help.
var getPoints = parseInt($('.awesome-points').text(), 10).toFixed(1);
alert(getPoints);
var addPoint = 1;
var newScore = getPoints + addPoint;
$('.awesome-points').text(newScore);
Mark
Share Improve this question edited May 14, 2012 at 16:13 Mark Hollas asked May 11, 2012 at 23:52 Mark HollasMark Hollas 1,1371 gold badge19 silver badges44 bronze badges 02 Answers
Reset to default 9.toFixed()
returns a string (see the MDN documentation), that's why getPoints + addPoint
performs string concatenation, not addition.
Call that method after you performed the addition:
// unary + converts any numerical string to a number (no matter whether float
// or int)
var getPoints = +$('.awesome-points').text();
var newScore = getPoints + 1;
$('.awesome-points').text(newScore.toFixed(1));
Or more concise :
$('.awesome-points').text(function(i, val) {
return (+val + 1).toFixed(1);
});
How this works:
Instead of passing a value to .text()
, you can pass a function. This function will be executed for each of the selected elements. The first argument passed to the function is the index of the element in the set of selected elements and the second argument is the current text content. The return value will be set as new text content.
This is a good way to read and write the text content, without invoking .text()
twice.
(+val + 1).toFixed(1)
is evaluated as follows: First val
is converted to a number through the unary plus (+val
) and then increased by one (+ 1
). .toFixed(1)
is called on the result of that operation.
+val
will return NaN
if the text cannot successfully be converted into a number. I assume this won't happen in your case, but this can easily be dealt with with the expression
return ((+val || 0) + 1).toFixed(1);
That means, in case +val
evaluates to false
(which includes NaN
), we use 0
instead. It's a mon pattern in JavaScript, see also What does "options = options || {}" mean in Javascript?.
i think what you want is something like this
var getPoints = $('.awesome-points').text();
//alert(getPoints);
var addPoint = 1;
var newScore = parseFloat(getPoints) + addPoint;
$('.awesome-points').text(newScore.toFixed(1));
seems that was your answer, toFixed() turns a numerical value into a string so by making the toFixed function later in your process
本文标签: javascriptjQuery adding a number to textStack Overflow
版权声明:本文标题:javascript - jQuery adding a number to text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744315095a2600222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论