-1

我试图增加我跨度的答案。

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

顺便说一句.21,它是 %21%,所以我们需要让它.21乘以它并得到总数。btwVAT荷兰

如果你看到 jqueryvar 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>
4

2 回答 2

1

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

于 2012-12-10T09:12:43.223 回答
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);
});
于 2012-12-10T09:47:52.737 回答