Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- How do i return a value from a closure function?


nelsonm - 1:16 pm on May 30, 2012 (gmt 0)


Thanks to you and Fotiman... I was able to get my code not only working but working better.

Here is the code you helped get working and improve:

function recalcTotals(){
var subtotal = 0.00, salestax = 0.00, taxrate = 0.00, surcharge = 0.00, salestotal = 0.00;
var taxEnabled = true, data, sdata;

// get each item cost and calculate subtotal.
$('#workorder-form #sa-table-1 tbody tr input[name="wi-Cost"]').each(function (){if($(this).val() != ''){subtotal += parseFloat($(this).val());}});

// calculate surcharge.
if(subtotal == 0.00){surcharge = 0.00;}else if(subtotal <= 100.00){surcharge = 5.00;}else{surcharge = subtotal * 0.06;} // gas surcharge calculation.

var updateForm = function() {
salestax = subtotal * (taxrate / 100);
salestotal = subtotal + salestax + surcharge;


// fill sales total fields. set all totals to 2 decimal places.
$('#workorder-form input#wo-SubTotal' ).val(subtotal.toFixed(2));
$('#workorder-form input#wo-SalesTax' ).val(salestax.toFixed(2));
$('#workorder-form label#wo-TaxRate' ).text(taxrate.toFixed(2)+'%');
$('#workorder-form input#wo-SurCharge' ).val(surcharge.toFixed(2));
$('#workorder-form input#wo-SalesTotal').val(salestotal.toFixed(2));
};


if(taxEnabled) {
data = {'_search':true, filters: '{"groupOp":"AND","rules":[{"field":"st.StateAbbrev","op":"eq","data":"'+$('#workorder-form select#cu-STID option:selected').text()+'"}]}', page:1, rows:40, searchField:'', searchOper:'', searchString:'', sidx:'st.StateAbbrev', sord:'asc'};
$.post('php/statetab-script.php', data, function(sdata){
var response = JSON.parse(sdata);
var row = response.rows['0'];
taxrate = parseFloat(row['cell']['4']);
updateForm();

});

}else{
updateForm();
}

}


again, thanks a lot.


Thread source:: http://www.webmasterworld.com/javascript/4459277.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com