我有一个用于自定义帖子类型的转发器自定义元框。我是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>\'