与来自无线电和复选框的JS一起添加两个值以获得总数



我想把两个值加在一起,在我的预订页面上得到一个总计。

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

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

<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

I have try:

<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

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

如果它是一个数字,那么它们可以相加。

如果是其他事情,请告诉我,也许我能找到一个办法。

相关内容

  • 没有找到相关文章

最新更新