1 回答
TA贡献1816条经验 获得超4个赞
您可以有一个变量来计算所有选定的 Ekstras 和一个用于无线电选择价格的变量。然后创建一个打印总结果的函数。
运行代码片段
$(document).ready(function() {
var price = 0;
var ekstras = 0;
$('input[type=radio]').on('click',function(){
price = parseFloat($(this).data('price'));
printTotal();
});
$(".ekstra").change(function(event) {
ekstras = 0;
$(".ekstra").find(":selected").each(function() {
ekstras += parseFloat($(this).data("price"));
});
printTotal();
});
// Function to print the total
function printTotal() {
var total = ekstras + price;
if (total == 0) {
$('#total').text('');
} else {
var net_fiyat2 = price + total;
$('#total').text(total.toFixed(2));
}
}
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.min.js"></script>
<div class="col-md-12">
<div class="container">
<div class="row">
<div class="col-md-12 pd-40">
<select multiple data-placeholder="Ekstra Malzeme Seçin.." class="ekstra">
<option data-price="1.10">Pizza Sos</option>
<option data-price="1.10">Beyaz Peynir</option>
<option data-price="1.10">Mozarella Peyniri</option>
<option data-price="1.10">Sucuk</option>
<option data-price="1.10">Sosis</option>
</select>
</div>
</div>
</div>
</div>
<input class="radio-button" type="radio" name="radio" data-price="5.12" />
<input class="radio-button" type="radio" name="radio" data-price="6.50" />
<div>
<span class="font-size-27 font-weight-bolds ml-1" id="total"></span>
</div>
添加回答
举报