计算转发器元框输入字段值并显示合计

时间:2018-12-13 作者:Kevin W.

我有一个用于自定义帖子类型的转发器自定义元框。我是Javascript新手,不知道如何计算每个重复输入字段的值并显示总的组合。我让它对第一个输入字段起作用,但它不会计算所有字段的值。这是我正在使用的代码。

    <script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".wpp-item-add.investment").click(function() {
            count = count + 1;
            $(\'#investment_here\').append(\'<div class="wpp-repeater-wrapper">Title: <input class="wpp-repeater-input" type="text" name="investment[\'+count+\'][title]" value="" placeholder="e.g. Web Design" />Amount: <input class="wpp-repeater-input" id="wpp-amount-value" type="text" name="investment[\'+count+\'][investment_item]" value="" onblur="calculate()" placeholder="e.g. 2500" /><span class="wpp-item-remove investment">Remove</span></div>\' );
            return false;
        });
        $(".wpp-item-remove.investment").live(\'click\', function() {
            $(this).parent().remove();
        });
    });
    calculate = function()
    {
    var resources = document.getElementById(\'wpp-amount-value\').value;
    document.getElementById(\'wpp_investment_total\').value = parseInt(resources);

    }
    </script>
我正在寻找的解决方案:如果我单击Add Item并有5个输入字段,它将计算每个字段的值,并将总数转到字段#wpp\\U investment\\U total。

HTML部分:

          <div class="wpp-input-container">
              <label class="wpp-label-repeater"><?php _e(\'Cost Items\') ?></label>
              <?php
              wp_nonce_field ( \'c_nonce_field\', \'c_wpnonce\');
                  $c = 0;
                  if ( count( $investment ) > 0 ) {
                      if(!empty($investment)) {
                          foreach( $investment as $investment_item_val ) {
                             if (!empty($investment_item_val)) {
                              foreach( $investment_item_val as $investment_item ) {
                                  if ( isset( $investment_item[\'title\'] ) || isset( $investment_item[\'investment_item\'] ) ) {
                                      printf( \'<div class="wpp-repeater-wrapper">Title: <input class="wpp-repeater-input" type="text" name="investment[%1$s][title]" value="%2$s" />Amount: <input class="wpp-repeater-input" id="wpp-amount-value" type="text" name="investment[%1$s][investment_item]" value="%3$s" onblur="calculate()" /><span class="wpp-item-remove timeline">%4$s</span></div>\', $c, $investment_item[\'title\'], $investment_item[\'investment_item\'], __( \'Remove\' ) );
                                      $c = $c +1;
                                  }
                              }
                            }
                          }
                      }
                  }
              ?>
              <span id="investment_here"></span>
              <div class="wpp-item-add investment" style="visibility: hidden; margin-bottom: -20px;"><?php _e(\'Add Item\'); ?></div>
              <div class="wpp-item-add investment add-button"><?php _e(\'Add Item\'); ?></div>
              </div>
对于任何正在寻找的人,这里有一个RiddleMeThis的有效解决方案

    calculate = function(){
        var total = 0;
        $(\'.wpp-repeater-input.amount\').each(function () {
            total += parseInt($(this).val());
        });
        var discount = document.getElementById(\'wpp_investment_discount\').value;
        var tax = document.getElementById(\'wpp_investment_tax\').value;

        document.getElementById(\'wpp_investment_total\').value = parseFloat(total)+parseInt(discount)+parseFloat(tax);
    }

Current code I have that doesn\'t subtract value of item deleted

 \'<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo esc_js( $c ); ?>;
        $(".wpp-item-add.pricing").click(function() {
            count = count + 1;
            $(\'#pricing_here\').append(\'<div class="wpp-repeater-wrapper">Title: <input class="wpp-repeater-input" type="text" name="pricing[\'+count+\'][title]" value="" placeholder="Web Design" />Amount: <input class="wpp-repeater-input amount" type="text" name="pricing[\'+count+\'][pricing_item]" value="0.00" onblur="calculate()" placeholder="" /><span class="wpp-item-remove pricing">Remove</span></div>\' );
            return false;
        });
        $(".wpp-item-remove.pricing").live(\'click\', function() {
            $(this).parent().remove();
        });
    });
    calculate = function(){
        var total = 0;
        $(\'.wpp-repeater-input.amount\').each(function () {
            total += parseFloat($(this).val());
        });

        total = parseFloat(total).toFixed(2);
        document.getElementById(\'wpp_pricing_total\').value = parseFloat(total);
    }
    </script>\'

1 个回复
最合适的回答,由SO网友:RiddleMeThis 整理而成

您需要遍历每个输入的值并将它们相加。你可以这样做using a each.

试试这个。用此函数替换计算函数。

calculate = function(){
    var total = 0;
    $(\'.wpp-repeater-input\').each(function () {
        total += parseInt($(this).val());
    });

    document.getElementById(\'wpp_investment_total\').value = parseInt(total);
}
另一方面:我会删除输入上的ID,您不应该有多个相同的ID。注意,我使用该类来定位输入。

相关推荐

输出metabox文本区域并避免li标记内的换行符

我添加了一个自定义元框,其中包含textarea 在数据库中。现在,如果我只是简单地像这样回应元框:$post_meta = get_post_meta($pid); $answer = $post_meta[\"answer\"][0]; echo $answer; html标记将被转义,文本将如下所示:blah blah blah <ul><li>blah blah</li><li>blah</li></ul&