0

我的值数组以 [100,200,300,500] 的形式正确返回我只想总结这些值,我做错了什么?

jQuery

$('select').focus(function () {
    previous = parseInt($(this).val());
}).change(function() {

    var item_cost = parseInt($(this).attr('cost'));
    
    values = $.map($('select[name="cookies"]'), function (e) {
            
        return $(e).val()* item_cost;
                     
        for (var i = 0; i < values.length; i++) {
            total += parseInt(values[i]);
            console.log(total);                              
        }
    });

    alert(values);
4

1 回答 1

2

Your code needs formatting. You missed a closing }); You need to declare var total = 0; as well. I'm not sure if that is elsewhere in your code.

$('select').focus(function() {
    previous = parseInt($(this).val());
}).change(function() {
    var item_cost = parseInt($(this).attr('cost'));

    values = $.map($('select[name="cookies"]'), function(e) {
        return $(e).val() * item_cost;
    }); // <-- RIGHT THERE

    for (var i = 0; i < values.length; i++) {
        total += parseInt(values[i]);
        console.log(total);
    }
});
alert(values);​
于 2012-11-21T17:44:48.033 回答