点击计算

时间:2014-10-02 12:51:20

标签: jquery html

我想对点击进行简单的计算,

我希望在每个单选按钮上都有一个值,比如10,20,30,如果点击它,那么大多数都计算在内,而不是



$('#1').click(function() {
   var first = 10;
   var second = 20;
   var third = 30;
   var total = first + second + third; //If isset..

   $("#total").val(total); // sets the total price input to the quantity * price
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type=radio id=1>
<input type=radio id=2>
<input type=radio id=3>
<p /><input id=total></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

jQuery("#1,#2,#3").click(function() {
   if (jQuery("#1").is(":checked")) { var first = 10; } else { var first = 0; }
   if (jQuery("#2").is(":checked")) { var second = 20; } else { var second = 0; }
   if (jQuery("#3").is(":checked")) { var third = 30; } else { var third = 0; }
   var total = first + second + third;

   jQuery("#total").val(total); // sets the total price input to the quantity * price
});

答案 1 :(得分:0)

HTML -

<input type=radio id=1>
<input type=radio id=2>
<input type=radio id=3>
<p /><input id=total></div>

JS代码 -

var first = 0;
var second = 0;
var third = 0;
$("input:radio").change(function(){
    if($(this).attr('id')==1)
    {
         first = 10;
    }
    if($(this).attr('id')==2)
    {
         second = 20;
    }
    if($(this).attr('id')==3)
    {
         third = 30;
    }

    var total = first + second +third;

    $("#total").val(total);

});

JSFiddle网址 - http://jsfiddle.net/Ashish_developer/rgmyhamz/