繁体   English   中英

将两个值与来自收音机和复选框的 JS 相加以获得总计

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

我正在尝试将两个值加在一起以在我的预订页面上获得总计。

用户使用单选按钮选择服务,然后可以使用复选框选择其他服务。

我正在使用 JavaScript 来计算复选框和单选按钮。

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>
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>

以上工作正常。

我正在努力解决的是总数,即将收音机添加到复选框中:

#servicetotal + #additionaltotal = #totalprice

我试过了:

<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>

但是我的页面没有输出。

<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>

如果有人能指出我正确的方向,我将不胜感激,并提前感谢您提供的任何帮助:o)

如果它是一个数组,请使用:

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]]

如果是字符串,请使用str1 + str2

如果它是一个对象,我不知道。

如果是数字,则可以将它们相加。

如果是其他任何事情,请告诉我,也许我可以找到方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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