jQuery减法不起作用

时间:2018-08-20 14:52:05

标签: javascript jquery

除减法之外的所有方法都有效

function add_culture(ele)
    {
        total=parseInt($('#total_price').val());
        culture_price=parseInt($('#culture_price').val());

        $('#total_price').val(culture_price);

        obj=$(ele).val();
        var myObject = eval('(' + obj + ')');

        price=parseInt(myObject['price']);
        $('#culture_price').val(price);

        $('#total_price').val(parseInt(total-culture_price));

        $('#total_price').val(total+price);

        count_remaining();

    }
我想知道是什么问题.......................................... ................................................... ................................................... ............

2 个答案:

答案 0 :(得分:0)

您在脚本中覆盖了total_price的值三次,请尝试在末尾仅附加最终的计算值。

您可能需要将计算出的总数存储在一个单独的变量中,以使情况保持清楚:

function add_culture(ele)
{
    var total = parseInt($('#total_price').val());
    var culture_price = parseInt($('#culture_price').val());
    var obj = $(ele).val();
    var myObject = eval('(' + obj + ')');
    var price = parseInt(myObject['price']);
    var final_total = (total-culture_price)+price;

    $('#culture_price').val(price);
    $('#total_price').val(final_total);

    count_remaining();
}

答案 1 :(得分:-1)

您已在第二行中替换了total_price的值

错误:

$('#total_price').val(parseInt(total-culture_price));
//$('#total_price').val(total+price); /* Comment this line in your code! */
相关问题