0
사용자가 무언가를 변경하고 모든 값을 다시 계산할 때 업데이트되는 양식이 있지만 일부 값은 PHP 세션에서 가져온 것이므로 페이지로드시 업데이트하고 올바른 값을 계산해야합니다 onchange()처럼.load()에서 jQuery 실행 함수
$(document).ready(function() {
$('.qty').change(function() {
update_amounts();
});
update_amounts(); //<--- must to be executed on document.ready()
});
function update_amounts() {
var sum = 0.0;
$('#myTable > tbody > tr').each(function() {
var qty = $(this).find('option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price)
sum+=amount;
$(this).find('.amount').text(''+amount);
});
//calculate the total to sum
$('.total').val(sum);
//calculate total + shipping and add VAT
var shipping = $('.shipping').val();
var total = $('.total').val();
var vat = $('.vat').val();
//calculate vat
var vat_value = ((total*vat)/100);
//calculate grand total
sub_total = (parseFloat(total)+parseFloat(shipping)).toFixed(2);
var grand_total = (parseFloat(sub_total)+parseFloat(vat_value)).toFixed(2);
$('.grandTotal').val(grand_total);
$('.modalTotal').val(grand_total);
}
하지만
누군가가 이유를 설명 할 수 없습니다 document.ready();
에 대한 몇 가지 값을 변경할 때이 코드 만 실행됩니다? 그리고 어떻게 기능을 할 수 update_amounts();
로드에서 시작할 수 있습니까?
올바른 것 같습니다. 어떤 오류가 있습니까? – eisbehr
표시된 코드는 문서 준비시 update_amounts를 실행해야합니다 ... 콘솔 오류가 발생합니까? –
아니요 ... 콘솔에 오류가 없으므로 해결책을 찾을 수 없습니다. – andreaem