javascript

时间:2018-10-05 02:28:03

标签: javascript

我想对功能的总和加两个来得到一个总和。但作为回报,它们只是合而为一,否则结果将返回NAN。

//function for display
    function update_price() {
      var row = $(this).parents('.item-row');
      var price = row.find('.cost').val().replace("₱" ,"") * row.find('.qty').val();
      price = roundNumber(price,2);
      isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("₱" +price);

      update_total();
      update_balance();
      update_ftotal();

    }

function update_total() {
      var total = 0;
      $('.price').each(function(i){
        price = $(this).html().replace("₱" ,"");
        if (!isNaN(price)) total += Number(price);
      });

  total = roundNumber(total,2);

  $('#subtotal').html("₱" +total);
  //$('#total').html("₱"+total); 

}

function update_balance() {
  var tax = $("#subtotal").html().replace("₱" ,"") * (0.12);
  tax = roundNumber(tax,2);

  $('.tax').html("₱" +tax);
}

function update_ftotal() {
  var sub , ax = 0;
  var sub = $("#subtotal").html();
  var ax = $('.tax').html();

  var due = sub + ax

  // due = roundNumber(due,2);

   $('.due').html(due);
}

这是前端,我在函数中使用类和ID

    <tr>
        <td colspan="3" class="blank"> </td>
        <td colspan="3" class="total-line">Subtotal:</td>
        <td class="total-value"><div id="subtotal" name="subtotal"></div></td>
    </tr>
    <tr>
        <td colspan="3" class="blank"> </td>
        <td colspan="3" class="total-line">12% Tax:</td>
        <td class="total-value"><div class="tax" id="tax"></div></td>
    </tr>

    <tr>
        <td colspan="3" class="blank"> </td> <!-- add tax result to the subtotal to get final total -->
        <td colspan="3" class="total-line balance">Total:</td>
        <td class="total-value balance"><div class="due" id="due"></div></td>
    </tr>

结果

enter image description here

2 个答案:

答案 0 :(得分:0)

sub和ax是字符串,因此plus运算符将它们连接起来。

尝试一下:

var due = parseFloat(sub) + parseFloat(ax);

答案 1 :(得分:0)

答案。

function update_ftotal() {

  var sub , ax = 0;
  var sub =  $(".subtotal").html(); 
  var ax =  $(".tax").html();

  var num1 = Number(sub);
  var num2 = Number(ax);

  var due = num1 + num2;

  due = roundNumber(due,2);

  $('.due').html(due);

}