同 $('.support:checked').val() 您可以获取当前所选单选按钮的值。我修改了你的代码以利用它。
$('.support:checked').val()
function myFunction() { var discountAmt = []; discountAmt[0] = "0"; discountAmt[1] = "5"; discountAmt[2] = "10"; discountAmt[3] = "15"; var n = $(".checkBoxLabel:checked").length; var multiply = 5 * n; var totalAmount = $('#totalAmount').html("$" + multiply); var discountAmount = $('#discountAmount').html("$" + discountAmt[n]); var youPay = $('#youPay').html(discountAmt[n]); return [totalAmount, discountAmount, youPay]; } var $totalCost = 0; function checkPromocode() { if ($('#procode').val().length >= 5) { var checkPromocode = $('#procode').val(); $.ajax({ type: "POST", url: baseUrl + "/C_Registration/checkPromocode", data: { checkPromocode: checkPromocode }, success: function(response) { var obj = JSON.parse(response); //alert(obj.message); var finalAmt = $totalCost; var codes = myFunction(); $('#promocodeMessage').html(obj.message); var sellingprice = finalAmt - (finalAmt * (obj.discount / 100)); $('#youPay').html(sellingprice.toFixed(2)); } //END success fn }); //END $.ajax } else { var codes = myFunction(); //recalculate the value depending on the currently selected radio button if ($('.support:checked').val() === '1') { var a = codes[2].html(); var totCost = a * 2; $('#youPay').html(totCost); } else { $('#youPay').html(codes[2].html()); } $('#promocodeMessage').html("Please check your promo code"); } } $(document).ready(function() { var $checks = $('.checkBoxLabel:checkbox'); $checks.click(function() { if ($checks.filter(':checked').length == 0) { $('.calculationStrip').hide(); } else { $('.calculationStrip').show(); var codes = myFunction(); $totalCost = codes[2].html(); $('#youPay').html(codes[2].html()); } }); $("#ckbCheckAll").click(function() { $(".checkBoxLabel").prop('checked', $(this).prop('checked')); var codes = myFunction(); $('.calculationStrip').show(); $totalCost = codes[2].html(); $('#youPay').html(codes[2].html()); }); $(".checkBoxLabel").change(function() { if (!$(this).prop("checked")) { $("#ckbCheckAll").prop("checked", false); } }); /*Customization script here*/ $('.support').on('click', function() { var codes = myFunction(); if ($(this).val() === '1') { var a = codes[2].html(); var totCost = a * 2; $totalCost = totCost; $('#youPay').html(totCost); } else { $totalCost = codes[2].html(); $('#youPay').html(codes[2].html()); } checkPromocode(); }); $('#procode').keyup(function() { checkPromocode(); }); });
<ul class="bookList"> <li><input type="checkbox" class="checkbox_round show_on_click select_all" id="ckbCheckAll">Select All</li> <li><input type="checkbox" name="book[]" value="1" class="all_checkbox checkBoxLabel ">Book One </li> <li><input type="checkbox" name="book[]" value="2" class="all_checkbox checkBoxLabel "> Book Two</li> <li><input type="checkbox" name="book[]" value="3" class="all_checkbox checkBoxLabel"> Book Three</li> </ul> <p>suport fields if user choose Yes then it will calculate the total cost mutiply by numbre of books. for example user chose two books then total cost is 10*2=20</p> <ul> <li><input type="radio" name="support" value="1" class="support">Yes</li> <li><input type="radio" name="support" value="0" class="support">No</li> </ul> <!--amount display here--> <p>Final Amount to pay:- $<span id="youPay"></span> </p> <input type="text" name="promocode" placeholder="Code here" class="form-control promoField" id="procode" autocomplete="off"> <p class="pull-right"><span id="promocodeMessage"></span></p> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>