I need to do some calculations only if the radio button has the attribute data-price
. Currently the calculation is executed all the time for all the radio buttons.
I Have attached My Code. Can any one help me on this?
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Hotel Beds</title>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
></script>
<script type="text/javascript">
$(document).ready(function() {
var values = {};
$('input:radio').change(function() {
var name = $(this).attr('name');
values[name] = parseFloat($(this).data('price'));
var total = 0.0;
$.each(values, function(i, e) {
total += e;
});
$('#total').text(total.toFixed(2));
});
});
</script>
</head>
<body>
<form id="form1">
<div>
<input type="radio" name="t1" data-price="25" checked="checked" />
<span>Price 25</span>
<input type="radio" name="t1" data-price="30" />
<span>Price 30</span>
<br />
<input type="radio" name="t2" data-price="40" />
<span>Price 40</span>
<input type="radio" name="t2" data-price="50" />
<span>Price 50</span>
<br />
<input type="radio" name="G" /><span>Male</span>
<input type="radio" name="G" /><span>Female</span>
<input id="total" type="text" />
</div>
</form>
</body>
</html>