【问题标题】:radio button multiply, addition and multiplication of values单选按钮乘法、加法和值的乘法
【发布时间】:2021-04-12 12:38:55
【问题描述】:

有人知道如何 + 和 * 选择单选值并显示结果吗?

在小提琴中> itemOne + itemTwo * itemThree =

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

非常感谢您的宝贵时间!

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;

【问题讨论】:

    标签: radio


    【解决方案1】:

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

    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 属性。

    【讨论】:

    • 感谢一百万康纳,这完全搞定了!干杯:)
    • 如果它对你有用,请随时接受答案
    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多