我正在使用此脚本实时计算span标记之间的价格:
$(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>
我必须在哪里放置:
toFixed(2)
这样价格总是显示2位小数?