Since you are doing the same operation regardless of which radio button you click, you can use one function:
var total1 = $('#total1');
var total2 = $('#total2');
var total3 = $('#total3');
function updateValues() {
// Get the selected values (default to zero if none selected).
var val1 = parseInt($('.itemOne:checked').val()) || 0;
var val2 = parseInt($('.itemTwo:checked').val()) || 0;
var val3 = parseInt($('.itemThree:checked').val()) || 0;
// Update the text inpus
total1.val(val1);
total2.val(val2);
total3.val(val3);
// Do calculation:
var calculation = (val1 + val2) * val3;
// Update your output:
document.getElementById('calculation').innerHTML = calculation;
}
// Use this whenever a radio changes
$('[type="radio"]').on('click', updateValues);
You won't need the inline onclick
attributes in your HTML with this either.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…