如何强制此外,而不是串联在JavaScript [复制](How to force addition

2019-07-05 23:02发布

这个问题已经在这里有一个答案:

  • 如何强制JS做的,而不是把两个字符串的数学 9个回答

我想添加的所有的卡路里含量在我的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

Answer 1:

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

你需要通过调用每个变量转换为数字parseFloat()左右各一个。

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


Answer 2:

也应该能够做到这一点:

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

这帮助我在一个不同的,但类似的,局面。



Answer 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; 声明了丢弃事件,并从这种改变分配statment

total = parseFloat(myInt1 + myInt2 + myInt3);

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

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



文章来源: How to force addition instead of concatenation in javascript [duplicate]