69

我正在尝试在我的 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上的视图源

4

3 回答 3

103

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

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

total = parseFloat(myInt1) + parseFloat(myInt2) + parseFloat(myInt3);
于 2012-12-19T13:59:19.697 回答
24

也应该能够做到这一点:

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

这帮助了我在不同但相似的情况下。

于 2015-03-28T03:14:03.393 回答
3

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

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

这使得您看起来像是在连接字符串,但实际上并非如此,您实际上是在将它们附加到元素

将其更改为

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

您需要更改 drop 事件,以便将元素的值替换为总数,您还需要跟踪总数是多少,我建议如下

$(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/

于 2012-12-19T14:21:34.683 回答