我试图通过ajax将出货价格总和加到我的购物车总价中。
到目前为止我所拥有的:
我想要做的是sum
这两个价值观。
示例:
Cart Total:
20.000
Shipping Cost:
10.000
Total:
30.000
控制器:
//Select shipping method to sum with total price of cart
public function totalPriceInTotal(Request $request, $postchoose) {
$totalPriceIn = $request->input('postchoose');
return response()->json($totalPriceIn);
}
路线:
Route::get('/totalPriceInTotal/{postchoose}','CartController@totalPriceInTotal');
查看:
//HTML
<div class="panel-body" id="totalPriceInTotal">
</div>
//JavaScript
<script>
$(document).ready(function() {
$('select[name="postchoose"]').on('change', function() {
var destinationPrice = $(this).val();
if(destinationPrice) {
$.ajax({
url: '{{ url('totalPriceInTotal') }}/'+encodeURI(destinationPrice),
type: "GET",
dataType: "json",
success:function(data) {
$('#totalPriceInTotal').empty();
$('#totalPriceInTotal').append('<p>' + {{Cart::getTotal()}} + $totalPriceIn + '</p>');
}
});
}else{
$('#totalPriceInTotal').empty();
}
});
});
</script>
我的网络结果:
PS:我需要的是
sum
37000
我的购物车总价。
这是我如何在下拉列表中获取运费数据并从中选择37000
:
success:function(data) {
$('#des').empty();
$('#des').append(
'<p>Destination: ' + data['meta']['destination']['province'] + ' , ' + data['meta']['destination']['type'] + ' , ' + data['meta']['destination']['city_name'] + ' , ' + data['meta']['destination']['postal_code'] +'</p>'
);
$.each(data.data, function(key, value) {
$('#info').empty();
$('select[name="postchoose"]').empty();
$('#info').append('<h3>'+ value['code'] + '<small>' + value['name'] +'</small></h3>');
$.each(value.costs, function(key2, value2) {
$.each(value2.cost, function(key3, value3) {
// number format
var number = value3['value'];
var nf = new Intl.NumberFormat('en-US', {
maximumFractionDigits:0,
minimumFractionDigits:0
});
var formattedNumber = nf.format(number);
// number format
$('select[name="postchoose"]').append('<option id="postchoose" class="form-control" name="postchoose" value="'+ value3['value'] +'">'+ value2['service'] + ' - ' + nf.format(number) + ' Rp' + ' - ' + value3['etd'] + ' hari' +'</option>');
});
});
});
}
答案 0 :(得分:4)
在你的脚本中使用这个成功希望这会有所帮助
<script>
$(document).ready(function() {
$('select[name="postchoose"]').on('change', function() {
var destinationPrice = $(this).val();
if(destinationPrice) {
$.ajax({
url: '{{ url('totalPriceInTotal') }}/'+ destinationPrice,
type: "GET",
dataType: "json",
success:function(data) {
var value= parseFloat(data)+parseFloat({{Cart::getTotal()}});
$('#totalPriceInTotal').empty();
$('#totalPriceInTotal').append('<p>' + value + '</p>');
}
});
}else{
$('#totalPriceInTotal').empty();
}
});
});
答案 1 :(得分:0)
假设$totalPriceIn
由模板引擎提供,这里可能有用。我使用本地存储。
$(document).ready(function() {
if(!localStorage.getItem('cartTotal')) {
var cartTotal = {{Cart::getTotal()}};
localStorage.setItem('cartTotal', cartTotal);
}
$('select[name="postchoose"]').on('change', function() {
var destinationPrice = $(this).val();
if(destinationPrice) {
$.ajax({
url: '{{ url('totalPriceInTotal') }}/'+encodeURI(destinationPrice),
type: "GET",
dataType: "json",
success:function(data) {
var cartTotalPrice = parseFloat(localStorage.getItem('cartTotal'));
$('#totalPriceInTotal').empty();
$('#totalPriceInTotal').append('<p>' + (cartTotalPrice + parseFloat(data)) + $totalPriceIn + '</p>');
localStorage.removeItem('cartTotal');
}
});
}else{
$('#totalPriceInTotal').empty();
}
});
});