【发布时间】:2018-09-02 07:33:06
【问题描述】:
我在<div> 中写出总和,但在我将值输入到输入框之前,它会显示NaN。
如何过滤掉它以避免 Nan?
app.js
$(document).ready(calculate);
$(document).on("keyup", calculate);
function calculate() {
var sum = 0;
var value1 = $("#a_price").val();
var value2 = $("#otherElemenet").val();
$(".o-money__answer").each(function(){
sum += parseFloat($(this).val()) * parseFloat(monetary_factor.replace(",", ".")) * parseFloat($(this).parent().find('input[type=hidden]').val());
});
$("#o-money__totalsum").html(sum.toFixed(2));
}
PHP
<form class="o-money__form" id="<?php echo $counter; ?>" method="post" action="#">
<?php $rows = get_field('pengar'); ?>
<?php if ($rows): ?>
<?php foreach($rows as $row): ?>
<div class="o-money__positionInput">
<div class="right50">
<label class="label labelInput">Antal <?php echo $row['typ_av_enhet']; ?></label>
</div>
<input type="text" value="" class="o-money__answer">
<input type="hidden" value="<?php echo $row['a_price'];?>">
</div>
<?php endforeach; ?>
<label id="o-money__totalsum"> 0</label>
</form>
<!--
<?php
if(have_rows('pengar')):
while(have_rows('pengar')) :
the_row();
$a_price = get_sub_field('a_price');
$monetary_factor = get_field('monetary_factor', 'option');
?>
-->
<?php
break;
endwhile;
endif;
?>
<?php endif; ?>
</form>
【问题讨论】:
-
您需要发布足够多的代码,以便我们可以将其复制到我们自己的机器上并重现您的问题。最好在jsfiddle.net 创建一个小提琴,但如果你不想这样做,至少在你的问题中发布一个工作示例。
标签: javascript php jquery wordpress advanced-custom-fields