Jquery 如何在函数中使用toFixed()进行计算
我使用此脚本实时计算span标记之间的价格:Jquery 如何在函数中使用toFixed()进行计算,jquery,tofixed,Jquery,Tofixed,我使用此脚本实时计算span标记之间的价格: $(document).ready(function () { var form = $('.bestelformulier'), elements = form.find('[data-price], [type="radio"], [type="checkbox"], select'), totalEl = $('.totaalprijs'); //function to calculate total var calcula
$(document).ready(function () {
var form = $('.bestelformulier'),
elements = form.find('[data-price], [type="radio"], [type="checkbox"], select'),
totalEl = $('.totaalprijs');
//function to calculate total
var calculateTotal = function () {
var total = 0;
$.each(elements, function () {
var field = $(this),
newVal;
// swith type omitted
newVal = parseFloat(newVal);
total += newVal;
});
totalEl.text(total);
};
// bind events
elements.on('change keyup', calculateTotal);
});
价格显示在:
<span class="totaalprijs"></span>
因此价格总是显示2位小数?
newVal
未定义。你遗漏了一些代码吗?totalEl.text(total.toFixed(2))
?totalEl.text(total.toFixed(2))
完成了任务!谢谢您
toFixed(2)