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

jquery - Javascript does not sum numbers - Stack Overflow

programmeradmin2浏览0评论

I'm not good using javascript/jquery so this is a dumb question! I coded this

    <li>
        <a href=".jpg"><img src=".jpg" width=50 height=50 /></a>
        <br><input name="p1" value="p1" prezzo="5" type="checkbox" />
        <br><label>Pianta &euro;</label><span class="price">5</span>
    </li>

    <li>
        <a href=".jpg"><img src=".jpg" width=50 height=50 /></a>
        <br><input name="p2" value="p2" prezzo="3" type="checkbox" />
        <br><label>Pianta &euro;</label><span class="price">3</span>
    </li>

var checkValues = $('input[type=checkbox]:checked').map(function() {
    return $(this).attr('prezzo');
}).get();

for(var i=0;i<checkValues.length;i++) {
    var tot = tot + checkValues[i];
    alert(tot);
}

In this way checkvalues contains array of numbers. So checking that 2 checkbox for example, on the browser appear 2 alert:

undefined5

and

undefined53

like if javascript is concatenating numbers like they would be strings!

how can i solve this?

I'm not good using javascript/jquery so this is a dumb question! I coded this

    <li>
        <a href="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg"><img src="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg" width=50 height=50 /></a>
        <br><input name="p1" value="p1" prezzo="5" type="checkbox" />
        <br><label>Pianta &euro;</label><span class="price">5</span>
    </li>

    <li>
        <a href="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg"><img src="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg" width=50 height=50 /></a>
        <br><input name="p2" value="p2" prezzo="3" type="checkbox" />
        <br><label>Pianta &euro;</label><span class="price">3</span>
    </li>

var checkValues = $('input[type=checkbox]:checked').map(function() {
    return $(this).attr('prezzo');
}).get();

for(var i=0;i<checkValues.length;i++) {
    var tot = tot + checkValues[i];
    alert(tot);
}

In this way checkvalues contains array of numbers. So checking that 2 checkbox for example, on the browser appear 2 alert:

undefined5

and

undefined53

like if javascript is concatenating numbers like they would be strings!

how can i solve this?

Share edited Jun 11, 2014 at 15:02 Giao Cerquone asked Jun 11, 2014 at 14:52 Giao CerquoneGiao Cerquone 2,4885 gold badges26 silver badges34 bronze badges 4
  • 1 var tot = tot + checkValues[i]; - the tot on right hand side is not initialized . hence the result. Put the declaration outside the loop var tot=0; – karthikr Commented Jun 11, 2014 at 14:55
  • Why do you think checkValues contains an array of numbers? Have you read the documentation for .attr()? It always returns a string (or undefined). – Ian Commented Jun 11, 2014 at 14:55
  • watch this exception Cannot read property 'map' of null, seems the problem is that map function recieves null or undefined values from checkboxes – martinezjc Commented Jun 11, 2014 at 15:00
  • You should change this for(var i=0;i<checkValues.length;i++) { var tot = tot + checkValues[i]; alert(tot); } to this: var i,tot=0; for(i=0;i<checkValues.length;i++) { tot = tot + +checkValues[i]; alert(tot); }. Javascript doesn't have block scope - so it's less confusing if you write it outside of the for block. Also, you should initialize tot to zero because in the first iteration tot's value is undefined – thedude Commented Jun 11, 2014 at 15:08
Add a ment  | 

4 Answers 4

Reset to default 3

Try to change:

var tot = tot + checkValues[i];

to this:

var tot = tot + +checkValues[i];

OR

change this:
return $(this).attr('prezzo');

to this:

return +$(this).attr('prezzo');

This will coerce the string to a number

Check this out:

var tot = 0;
for(var i=0;i<checkValues.length;i++) {
    tot = tot + parseInt(checkValues[i]);
    alert(tot);
}

You keep reinitializing tot (which isn't strictly speaking a problem in JS, but it's bad form), plus you're initializing it to undefined initially, since you're defining it as itself. Initialize it to 0 before the loop.

Javascript is a duck typed (loosely typed) language. If it thinks they're strings, it'll concat them. Try forcing them into the data type you need. If they're integers, use parseInt(varName) + parseInt(varName2).

http://www.w3schools./jsref/jsref_parseint.asp

发布评论

评论列表(0)

  1. 暂无评论