单选按钮值的乘法、加法和乘法

radio button multiply, addition and multiplication of values

有谁知道如何 + 和 * select编辑无线电值并显示结果?

在 fiddle > itemOne + itemTwo * itemThree =

设法 select 值并计算出数学,但很难将它们整合在一起。

非常感谢抽出时间!

G

https://jsfiddle.net/omx617h8/

$(".itemOne").click(function() {
  var total = 0;
  $(".itemOne:checked").each(function() {
    total += parseInt($(this).val());
  })
  $("#total1").val(total);
});

$(".itemTwo").click(function() {
  var total = 0;
  $(".itemTwo:checked").each(function() {
    total += parseInt($(this).val());
  })
  $("#total2").val(total);
});

$(".itemThree").click(function() {
  var total = 0;
  $(".itemThree:checked").each(function() {
    total += parseInt($(this).val());
  })
  $("#total3").val(total);
});




var a = 5;
var b = 2;
var c = 2;
var z = (a + b) * c;
document.getElementById("calculation").innerHTML = z;

由于无论单击哪个单选按钮,都在执行相同的操作,因此可以使用一个功能:

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);

您也不需要 HTML 中的内联 onclick 属性。