How can I use jQuery with Laravel and Ajax to update a div's price based on a user-selected shipment service?

huangapple go评论78阅读模式
英文:

How can I use jQuery with Laravel and Ajax to update a div's price based on a user-selected shipment service?

问题

我想要更新div中的价格,以使用用户选择的运输服务。

<p class="mb-1"><b id="shipping-value">Rp. 0</b></p>

当用户选择运输服务时,有人可以帮我吗?

英文:

i have input select like this :

<div class="mb-4 pb-2">
       <select class="form-select" id="shipment-price" required
       @if(Cart::content()->isEmpty()) disabled @endif>
          <option value="" disabled selected style="display: none;">- Select Delivery Service -</option>
       @forelse ($shipment as $shipping)
          <option value="{{ $shipping->id }}">{{ $shipping->name }} ({{ presentPrice($shipping->price) }})</option>
       @empty
          <option value="0">-</option>
       @endforelse
       </select>
    </div>

and div like this

<div class="row justify-content-between">
       <div class="col">
           <p class="mb-1"><b>Shipping</b></p>
       </div>
       <div class="flex-sm-col col-auto">
           <p class="mb-1"><b id="shipping-value">Rp. 0</b></p>
       </div>
   </div>

i want to update div price to use user selected shipment service

<p class="mb-1"><b id="shipping-value">Rp. 0</b></p>

when user select the shipment service, can somebody help me ??

答案1

得分: 0

shipment-priceonChange 事件会触发对应的路由,返回 shipment value

$(document).ready(function(){
        $('#shipment-price').on('change', function(e){ //on change of shipment-price 
		let shipmentValue = $('#shipment-price').val(); // the dropdown item selected value
		$.ajax({
			type : "GET",
			dataType : 'json', 
			url : "{{route('pricevalue', ['id='+shipmentValue])}}",
			success : function(data){
				console.log(data);
                if(data.status){ //from your response 
				    $('#shipping-value').val(data.amount) // you can check the console to be sure of the response  
                }
			},error: function(e){
				console.log(e);
			},
		})
	});
}) 

你的路由会是这样的:

Route::get('/get-shipment-price/{id}', [ShipmentController::class, 'getShipmentPrice'])->name('pricevalue');

而在 YourController 中的 getShipmentPrice 函数会是这样的:

public function getShipmentPrice(Request $request)
    {
        $shipmentPrice = Shipment::where('shipment-type', $request->id)->first();
        if ($shipmentPrice) {
            return response()->json([
                'status' => true,  'data' => $shipmentPrice
            ]);
        }
        return response()->json([
            'status' => false, 'message' => 'Error',
        ]);
    }
英文:

onChange of shipment-price, you would have a route to return the shipment value.

$(document).ready(function(){
        $(&#39;#shipment-price&#39;).on(&#39;change&#39;, function(e){ //on change of shipment-price 
		let shipmentValue = $(&#39;#shipment-price&#39;).val(); // the dropdown item selected value
		$.ajax({
			type : &quot;GET&quot;,
			dataType : &#39;json&#39;, 
			url : &quot;{{route(&#39;pricevalue&#39;, [&#39;id&#39;=&gt;&quot;+shipmentValue+&quot;])}}&quot;,
			success : function(data){
				console.log(data);
                if(data.status){ //from your response 
				    $(&#39;#shipping-value&#39;).val(data.amount) // you can check the console to be sure of the response  
                }
			},error: function(e){
				console.log(e);
			},
		})
	});
}) 

Your route would look like:

Route::get(&#39;/get-shipment-price/{id}&#39;, [ShipmentController::class, &#39;getShipmentPrice&#39;])-&gt;name(&#39;pricevalue&#39;);

While your getShipmentPrice function in YourController would look like:

public function getShipmentPrice(Request $request)
    {
        $shipmentPrice = Shipment::where(&#39;shipment-type&#39;, $request-&gt;id)-&gt;first();
        if ($shipmentPrice) {
            return response()-&gt;json([
                &#39;status&#39; =&gt; true,  &#39;data&#39; =&gt; $shipmentPrice
            ]);
        }
        return response()-&gt;json([
            &#39;status&#39; =&gt; false, &#39;message&#39; =&gt; &#39;Error&#39;,
        ]);
    }

huangapple
  • 本文由 发表于 2023年6月5日 00:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定