如何在javascript中强制添加而不是连接

时间:2012-12-19 13:56:08

标签: jquery scope concatenation addition

我正在尝试在我的javascript中添加所有卡路里内容:

$(function() {
    var data = [];

    $( "#draggable1" ).draggable();
    $( "#draggable2" ).draggable();
    $( "#draggable3" ).draggable();

    $("#droppable_box").droppable({
        drop: function(event, ui) {
            var currentId = $(ui.draggable).attr('id');
            var total = 0;
            data.push($(ui.draggable).attr('id'));

            if(currentId == "draggable1"){
            var myInt1 = parseFloat($('#MealplanCalsPerServing1').val());
            }
            if(currentId == "draggable2"){
            var myInt2 = parseFloat($('#MealplanCalsPerServing2').val());
            }
            if(currentId == "draggable3"){
            var myInt3 = parseFloat($('#MealplanCalsPerServing3').val());
            }
        if ( typeof myInt1 === 'undefined' || !myInt1 ) {
        myInt1 = parseInt(0);
        }
        if ( typeof myInt2 === 'undefined' || !myInt2){
        myInt2 = parseInt(0);
        }
        if ( typeof myInt3 === 'undefined' || !myInt3){
        myInt3 = parseInt(0);
        }
        total = parseFloat(myInt1 + myInt2 + myInt3);
        $('#response').append(total);
        }
    });
    $('#myId').click(function(event) {
        $.post("process.php", ({ id: data }), function(return_data, status) {
            alert(data);
            //alert(total);
        });
    });
});

不是添加变量而是连接它们。我已经尝试过使用parseInt,parseFloat和Number但我仍然只是连接而不是添加。请查看http://maureenmoore.com/momp_112412/121912_800.html

上的查看来源

3 个答案:

答案 0 :(得分:68)

您的代码连接三个字符串,然后将结果转换为数字。

您需要通过在每个变量周围调用parseFloat()每个变量转换为数字。

total = parseFloat(myInt1) + parseFloat(myInt2) + parseFloat(myInt3);

答案 1 :(得分:8)

也应该能够做到这一点:

total += eval(myInt1) + eval(myInt2) + eval(myInt3);

这使我处于一种不同但相似的情况。

答案 2 :(得分:1)

以下语句将值附加到id为response

的元素
$('#response').append(total);

这使得它看起来像是在连接字符串,但你不是,你实际上是将它们附加到元素

将其更改为

$('#response').text(total);

你需要更改drop事件,以便它用total替换元素的值,你还需要跟踪总数是多少,我建议如下所示

$(function() {
    var data = [];
    var total = 0;

    $( "#draggable1" ).draggable();
    $( "#draggable2" ).draggable();
    $( "#draggable3" ).draggable();

    $("#droppable_box").droppable({
        drop: function(event, ui) {
        var currentId = $(ui.draggable).attr('id');
        data.push($(ui.draggable).attr('id'));

        if(currentId == "draggable1"){
            var myInt1 = parseFloat($('#MealplanCalsPerServing1').val());
        }
        if(currentId == "draggable2"){
            var myInt2 = parseFloat($('#MealplanCalsPerServing2').val());
        }
        if(currentId == "draggable3"){
            var myInt3 = parseFloat($('#MealplanCalsPerServing3').val());
        }
        if ( typeof myInt1 === 'undefined' || !myInt1 ) {
            myInt1 = parseInt(0);
        }
        if ( typeof myInt2 === 'undefined' || !myInt2){
            myInt2 = parseInt(0);
        }
        if ( typeof myInt3 === 'undefined' || !myInt3){
        myInt3 = parseInt(0);
        }
        total += parseFloat(myInt1 + myInt2 + myInt3);
        $('#response').text(total);
        }
    });

    $('#myId').click(function(event) {
        $.post("process.php", ({ id: data }), function(return_data, status) {
            alert(data);
            //alert(total);
        });
    });
});

我将var total = 0;语句移出了drop事件,并从此

更改了赋值语句
total = parseFloat(myInt1 + myInt2 + myInt3);

到这个

total += parseFloat(myInt1 + myInt2 + myInt3);

以下是一个工作示例http://jsfiddle.net/axrwkr/RCzGn/