2 回答
TA贡献1836条经验 获得超3个赞
此代码可能会有所帮助...我已添加data-target属性以使用 ID 定位特定价格。
jQuery(document).ready(function($) {
$(document).on('click', '.extraRoomOption:checked', function(e){
var targetPrice = $(this).data('target');
$('#'+targetPrice).text($(this).val());
grandTotal();
});
function grandTotal(){
var total = 0;
$('.extraRoomOption:checked').each(function(e){
total += parseFloat($(this).val());
});
$('#totalPrice').text(total);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Maggie's room type</h3>
<p>would you like your own room?</p>
<label>
<input type="radio" class="extraRoomOption option_yes" name="extraRoom1" id="y1_extraRoomInput" data-target="p1_price" value="150000">
<b>Yes, please</b>
</label>
<label>
<input type="radio" class="extraRoomOption option_no" name="extraRoom1" id="n1_extraRoomInput" value="125000" data-target="p1_price" checked>
<b>No, thanks</b>
</label>
<hr>
<h3>Esther's room type</h3>
<p>would you like your own room?</p>
<label>
<input type="radio" class="extraRoomOption option_yes" name="extraRoom2" id="y2_extraRoomInput" data-target="p2_price" value="150000">
<b>Yes, please</b>
</label>
<label>
<input type="radio" class="extraRoomOption option_no" name="extraRoom2" id="n2_extraRoomInput" value="125000" data-target="p2_price" checked>
<b>No, thanks</b>
</label>
<br>
<hr><hr>
<h2>Room type</h2>
<p>Maggie (Twin Room) And Price is <span id="p1_price">125000</span></p>
<p>Esther (Twin Room) And Price is <span id="p2_price">125000</span></p>
<p><b>Total Price is <span id="totalPrice">250000</span></b></p>
TA贡献1911条经验 获得超7个赞
您需要添加表单标签(使用它更容易处理输入字段)。请注意,html 代码与您的示例几乎相同,只是添加了表单标签。
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('radio-form');
var inputs = form.querySelectorAll('input[type="radio"]');
var p1 = document.getElementById('p1_price');
var p2 = document.getElementById('p2_price');
var total = document.getElementById('totalPrice');
function setPrice() {
var room1 = form.elements['extraRoom1'];
var room2 = form.elements['extraRoom2'];
p1.innerHTML = room1.value;
p2.innerHTML = room2.value;
total.innerHTML = +room1.value + +room2.value;
};
Array.prototype.forEach.call(inputs, function(input) {
input.addEventListener('change', setPrice);
});
setPrice();
});
<h3>Maggie's room type</h3>
<p>would you like your own room?</p>
<form id="radio-form">
<label>
<input type="radio" class="extraRoomOption option_yes" name="extraRoom1" id="y1_extraRoomInput" value="150000">
<b>Yes, please</b>
</label>
<label>
<input type="radio" class="extraRoomOption option_no" name="extraRoom1" id="n1_extraRoomInput" value="125000" checked>
<b>No, thanks</b>
</label>
<hr>
<h3>Esther's room type</h3>
<p>would you like your own room?</p>
<label>
<input type="radio" class="extraRoomOption option_yes" name="extraRoom2" id="y2_extraRoomInput" value="150000">
<b>Yes, please</b>
</label>
<label>
<input type="radio" class="extraRoomOption option_no" name="extraRoom2" id="n2_extraRoomInput" value="125000" checked>
<b>No, thanks</b>
</label>
</form>
<br>
<hr>
<hr>
<h2>Room type</h2>
<p>Maggie (Twin Room) And Price is <span id="p1_price">125000</span></p>
<p>Esther (Twin Room) And Price is <span id="p2_price">125000</span></p>
<p><b>Total Price is <span id="totalPrice">250000</span></b></p>
- 2 回答
- 0 关注
- 142 浏览
添加回答
举报