简体   繁体   中英

Adding Two Values together with JS from radios and checkboxes to get grand total

I am trying to add two values together to get a grand total on my booking page.

The user selects a service using radio buttons and then can select additional services by using checkboxes.

I am using JavaScript to calculate the checkboxes and the radio buttons.

HTML

<div class="row" id="service_calculate">
    @foreach($pricing as $price)
    <div class="col-lg-4 col-md-6">
        <div class="pricing-table border rounded bg-white text-center">
            <h5 class="pricing-plan rounded-top text-uppercase bg-custom text-light p-3 mb-0">{{$price->service_name}}</h5>
            <div class="price-value py-5 bg-light">
                <h4 class="mb-0">£{{$price->service_cost}}</h4>
            </div>
            <div class="pricing-features p-4">
                <div class="">
                    <label class="btn btn-outline-custom"><input type="radio" name="service" value="{{$price->service_cost}}"> Select {{$price->service_name}}</label>
                </div>
            </div>
        </div>
        <!--end table-->
    </div>
    <!--end col-->
    @endforeach
</div>
<!--end row-->
<div id="calculate_additional">
    <h4 class="mt-5 mb-2">Select Additional Work</h4>
    @foreach($additional as $addition)
    <div class="boxed-element mt-4">
        <div class="boxed-style-1 border  rounded p-4 mb-2">
            <h5 class="text-muted">{{$addition->additional_work}} -- <span class="text-custom">£{{$addition->cost}}</span>
                <label class="btn btn-custom float-right">
                    <input class="checkbox" type="checkbox" name="{{$addition->additional_work}}" value="{{$addition->cost}}"> Select</label></h5>
        </div>
    </div>
    @endforeach
    <div class="boxed-element mt-4">
        <div class="boxed-style-shadow bg-light shadow border rounded p-4 mb-2">
            <h5 class="text-muted" id="servicetotal">Total Service Cost
                <span class="text-custom float-right">0</span></h5>
        </div>
    </div>
    <div class="boxed-element mt-4">
        <div class="boxed-style-shadow bg-light shadow border rounded p-4 mb-2">
            <h5 class="text-muted" id="additionaltotal">Total Additional Cost
                <span class="text-custom float-right">0</span></h5>
        </div>
    </div>
</div>

Script

<script>
jQuery($ => {
    const calcTotal = () => $checkboxess.filter(':checked').map((i, el) => parseFloat(el.value)).get().reduce((s, v) => s + v, 0);
    let $checkboxess = $("#service_calculate :radio").on('change', e => {
        $("#servicetotal span").text(calcTotal().toFixed(2));
    });
});
</script>
<script>
jQuery($ => {
    const calcTotal = () => $radiobuttons.filter(':checked').map((i, el) => parseFloat(el.value)).get().reduce((s, v) => s + v, 0);
    let $radiobuttons = $("#calculate_additional :checkbox").on('change', e => {
        $("#additionaltotal span").text(calcTotal().toFixed(2));
    });
});
</script>

The above is working fine.

What I am struggling with is the total ie adding the radio's to the check boxes:

#servicetotal + #additionaltotal = #totalprice

I have tried:

<script>
jQuery(document).ready(function($) {
    var sertot = $("#servicetotal").html();
    var addtot = $("#additionaltotal").html();

    function addtotals(sertot, addtot) {
        var sum = parseInt(sertot) + parseInt(addtot);
        return sum;
    };
    var sum = addtotals(sertot, addtot);
    $("#totalprice span").append(sum);
});
</script>

But I am getting no output in my page.

<div class="boxed-element mt-4">
    <div class="boxed-style-shadow bg-light shadow border rounded p-4 mb-2">
        <h5 class="text-muted" id="totalprice">Grand Total
            <span class="text-custom float-right">0</span></h5>
    </div>
</div>

If someone could point me in the right direction it would be much appreciated, and thank you in advance for any help you can give :o)

If it is an array, use this:

function stitchArray(arrarr){
    var numArr = arrarr.length;
    var total = [];
    for(var i = 0; i < numArr; i++){
        for(var j = 0; j < arrarr[i].length; j++){
            total.push(arrarr[i][j]);
        }
    }
    return(total);
}
var arr1 = ["I love coding",404,true,{name: "Joe", age: 90}];
var arr2 = [false,"hello world",9999,["derp","nah"]];
alert(stitchArray([arr1,arr2])); //[I love coding,404,true,[objectObject],false,hello world,9999,[derp,nah]]

If it is a string, use str1 + str2 .

If it is an object, I don't know.

If it is a number, then they could be added together.

If it is any other thing, please tell me and maybe I could find a way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM