2012-12-10 47 views
-1

我試圖乘以我的跨度答案。jquery乘以跨度的值並獲得總數

的值,例如<span id="total_f">210</span>

和BTW .21這是%21%,所以我們需要讓.21繁殖,並得到總。 btw是在荷蘭

VAT,如果你看到了jQuery var get_total我沒有乘mul

是我的jQuery的錯嗎?

這是我的jQuery

$(document).ready(function(){ 
    var sum = 0; 
    $('input[name=sumof]').each(function(){ 
     sum += parseInt($(this).val()); 
    }); 
    $('input[name=total_f]').val(sum); 
    $('#total_f').text(sum); 

    var mul = parseInt($('input[name=total_f]').val()); 

    var get_total = mul * parseInt($('input[name=btw]').val()); 

    $('input[name=s_btw]').val(get_total); 
    $('#s_btw').text(get_total); 
}); 

我的HTML

<table cellpadding="5" cellspacing="0" width="100%"> 
    <tr> 
     <td><strong>Sub Total</strong></td> 
     <td> 
     <?php 
      if ($get_total_computation['currency'] == 'euro') { 
       $msg_tot = '&euro;'; 
      } elseif ($get_total_computation['currency'] == 'usd') { 
       $msg_tot = '&#36;'; 
      } 
      echo $msg_tot; 

     ?>      
      <span id="total_f"></span> 
      <input type="hidden" name="total_f" value="" /> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <? 
      echo $get_total_computation['quo_btw']; 
      $get_per = explode('%', $get_total_computation['quo_btw']); 
      ?> 
      <input type="hidden" name="btw" value=".<?=$get_per[0];?>" /> 
     </td> 
     <td> 
      <span id="s_btw"></span> 
      <input type="hidden" name="s_btw" value="" /> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Total</strong></td> 
     <td><?=$btw;?></td> 
    </tr> 
</table> 
+1

爲什麼標記'php'? –

+0

你爲什麼不直接在PHP中計算增值稅並將其寫入頁面? –

+0

@Jack的東西是其他值也在jQuery中使用PHP,因爲一些值在mysql上調用 – Butternut

回答

1

使用parseFloat解析包含十進制數的值。在你的情況下,btw包含值.21,其中parseInt是不合適的。

+0

謝謝先生它的作品! – Butternut

0

如果您更新HTML到這樣的事情(這將是在JS性能越好):

<table cellpadding="5" cellspacing="0" width="100%"> 
    <tr> 
    <td><strong>Sub Total</strong></td> 
    <td> 
     <?php 
     // cleaner way to do what you 
     // were trying to do here before 
     $currencies = array(
      'euro' => '&euro;', 
      'usd' => '&#36;' 
     ); 
     echo $currencies[$get_total_computation['currency']]; 
     ?>      
     <span id="total_f"> 
     <input type="hidden" name="total_f" /> 
     </span> 
    </td> 
    </tr> 
    <tr> 
     <td> 
      <?php echo $get_total_computation['quo_btw']; ?> 
      <!-- 
      removed the hidden input that was here as it's not 
      necessary unless you need to submit the btw value 
      with the form? 
      --> 
     </td> 
     <td> 
      <span id="s_btw"> 
      <input type="hidden" name="s_btw" data-btw="<?php echo $get_total_computation['quo_btw']; ?>" /> 
      </span> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Total</strong></td> 
     <td><?php echo $btw; ?></td> 
    </tr> 
</table> 

然後,您可以使用類似下面的做你需要的東西:

$(document).ready(function() { 
    var $subTotal = $('#total_f'), 
     $total = $('#s_btw'), 
     subTotal = 0, 
     total, btw; 

    $('input[name=sumof]').each(function() { 
    // can multiply by 1 to convert to 
    // number instead of using parseInt(); 
    subTotal += (this.value * 1); 
    }); 

    // btw is stored in #s_btw's data attr so 
    // we can get to it like this, remove the % 
    // symbol and multiply by 1 to convert to 
    // a number instead of using parseInt(); 
    btw = ($total.data('btw').replace('%', '') * 1); 

    // total is subTotal + 21% of subTotal so: 
    total = subTotal + ((subTotal * btw)/100); 

    // update the UI 
    $total.append(total); 
    $subTotal.append(subTotal); 
    $total.find('input').val(total); 
    $subTotal.find('input').val(subTotal); 
});