jquery乘以span的值并得到总数

时间:2012-12-10 09:00:35

标签: php jquery

我试图将我的跨度的答案倍增。

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

和btw .21它是%21%所以我们需要让它.21来乘以它并得到总数。荷兰btwVAT

如果您看到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>

2 个答案:

答案 0 :(得分:1)

使用parseFloat来解析包含十进制数的值。在您的情况下,btw包含.21不适合的值parseInt

答案 1 :(得分:1)

如果你将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);
});