通过 Ajax 价格数据形成输入文本,但不是所选产品的价格,而是始终只带来第一个价格

Through Ajax price data comes to form input text, but not the selected product's price, instead only the first price is always brought

我准备了一份销售记录表,您首先要 select product/stock_name。生成此 selection 后,它应使用应从 DB 中的股票 table 获取的产品s/stock 价格填充名为 unit_selling 价格的字段。发生这种情况的部分原因是第一个 stock/product 的价格被获取并且仅出现在浏览器控制台中。帮助我获取特定产品的价格,并使其不仅出现在控制台的表格中。我附上了一张数据库图片。

Create.blade.php

@extends('layouts.app')

@section('content')
    <br>
    <h1>Add Sale</h1>
    {!! Form::open(['action' => 'SalesController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}

    <div class="form-group">
    <label >Product Name</label>
    <select name="product_name" class="form-control" id="stock_name">
        <option>Select Product Name</option>
        @foreach ($stocks as $stock)
            <option value="{{ $stock->stock_name }}" >{{ $stock->stock_name}}</option>
        @endforeach
    </select>
</div>

<script>
    $(document).ready(function() {
        $("#stock_name").on('change', function () {
            let element = $(this);
            $.ajax({
                url: '/sales-price/getunitsellingprice',
                method: 'GET',
                data: {
                    'stock_name' : element.val(),
                },
                success: function (response) {
                       $("#unit_selling_price").val(response);
                },
            });
        });
    });
</script>

<div class="form-group">
    {{Form::label('unit_selling_price', 'Unit Selling Price')}}
    {{Form::text('unit_selling_price', '', ['class' => 'form-control', 'placeholder' => 'Unit Selling Price', 'id' => 'unit_selling_price'])}}
</div>
@endsection

SalesController.php

public function getUnitSellingPrice(Request $request)
    {
        $stock_name = $request->input("stock_name");
        if($stock_name = null){
            return null;
        }
        $stock = Stock::where('stock_name', 'like', "%".$stock_name."%")->first();
        if ($stock == null) {
            return null;
        }
        return response()->json($stock->unit_selling_price);
    }

数据库截图:股票 table

https://slack-files.com/TP02E38GN-FP2G2BM2S-5cb95174a0

表格截图

https://slack-files.com/TP02E38GN-FP287UP55-0e4b4a0261

这里可以省略触发器。并像这样重写该行

$("#unit_selling_price").val(response.data);

另外,要获取所选的选项值,您必须使用

let element = $(this+' :selected');

您需要尝试以下代码。

在路由文件中(web.php)

Route::any('sales-price/getunitsellingprice/{stock_id}','SalesController@getUnitSellingPrice');

在控制器中:

public function getUnitSellingPrice(Request $request, $stock_id)
{

   $stock = Stock::where('stock_id', $stock_id)->first();
   if ($stock == null) {
      return null;
   }

   return response()->json($stock->unit_selling_price);
}

更新您的 blade 文件。

<div class="form-group">
    <label>Product Name</label>
    <select name="product_name" class="form-control" id="stock_name">
    <option>Select Product Name</option>
        @foreach ($stocks as $stock)
        <option value="{{ $stock->stock_id }}">{{ $stock->stock_name}}</option>
        @endforeach
    </select>
</div>

<script>
$(document).ready(function () {
    $("#stock_name").on('change', function () {
    var stock_id = $(this).val();       
        $.ajax({
            //url: MyAppUrlSettings.MyUsefulUrl,
            url: '/sales-price/getunitsellingprice/'+stock_id,
            method: 'GET',              
            success: function (response) {
            console.log(response);
            $("#unit_selling_price").val(response);
            },
        });
    });
});
</script>

谢谢