只有單選按鈕具有屬性data-price
時,我需要執行一些計算。目前,所有單選按鈕始終執行計算。僅當元素具有特定屬性時才執行更改事件
我已附上我的密碼。誰可以幫我這個事?
<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>
7秒快,那麼不妨給予好評你的。 – adeneo
@adeneo :-) ... –