edit: replies! unfortunately using .trigger('change') or .change() manually run event doesn't work properly. if user changes value of 1 set of radio buttons, doesn't change other, maths doesn't add up. other suggestions?
i have this form
it works want to, jquery function run on page load when user changes radio button selection. how this?
var price=$("input[name='courierrepairprice']").val(); price = number(price.replace(/[^0-9\.]+/g,"")); var postage_out=0; var postage_in=0 $("input[name='outwardpostage']").change(function() { postage_out=$(this).val(); tot_price=parsefloat(price,10)+parsefloat(postage_out,10)+parsefloat(postage_in,10) tot_price=tot_price.tofixed(2) $('.tot_price').text('£'+tot_price); }); $("input[name='returnpostage']").change(function() { postage_in=$(this).val(); tot_price=parsefloat(price,10)+parsefloat(postage_out,10)+parsefloat(postage_in,10) tot_price=tot_price.tofixed(2) $('.tot_price').text('£'+tot_price); });
use .trigger('change') or .change() manually simulate event
var price=$("input[name='courierrepairprice']").val(); price = number(price.replace(/[^0-9\.]+/g,"")); var postage_out=0; var postage_in=0; function calculate(){ console.log('calc', price, postage_out, postage_in) var tot_price = price + postage_out + postage_in; tot_price=tot_price.tofixed(2) $('.tot_price').text('£' + tot_price); } $("input[name='outwardpostage']").change(function() { postage_out = parsefloat($(this).val()); calculate(); }).filter(':checked').trigger('change'); $("input[name='returnpostage']").change(function() { postage_in = parsefloat($(this).val()); calculate(); }).filter(':checked').trigger('change');
demo: fiddle
Comments
Post a Comment