【问题标题】:Through Ajax price data comes to form input text, but not the selected product's price, instead only the first price is always brought通过Ajax价格数据来形成输入文本,而不是选择产品的价格,而是总是带第一个价格
【发布时间】:2020-02-01 16:25:57
【问题描述】:

我准备了一份销售记录表,您首先选择产品/库存名称。一旦做出此选择,它应该使用应该从数据库中的库存表中获取的该产品/股票的价格填充下面名为 unit_sales 价格的字段。发生这种情况的部分原因是第一个库存/产品的价格被获取并且只出现在浏览器控制台中。帮助我获取特定产品的价格并使其不仅出现在控制台中。我附上了一张数据库图片。

创建.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);
    }

数据库截图:股票表

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

表单截图

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

【问题讨论】:

    标签: php ajax laravel php-7 laravel-5.8


    【解决方案1】:

    您需要尝试以下代码。

    在路由文件中(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);
    }
    

    更新您的刀片文件。

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

    谢谢

    【讨论】:

      【解决方案2】:

      您可以在此处省略触发器。并像这样重写这一行

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

      还要获得您必须使用的选定选项值

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

      【讨论】:

      • 差不多了。 .. 是什么让它给表单域带来了价格 $("#unit_sales_price").val(response) 现在的问题是:当我选择任何产品时,只有第一个产品的价格被带到表单域。即使我选择最后一个产品,我也会在这里得到 15.0000 检查数据库:files.slack.com/files-pri/TP02E38GN-FP26UB023/… 到目前为止的表格:slack-files.com/TP02E38GN-FNNSFU62X-5a70b3e316
      • @shibab 我删除了我的 let element = $(this);并把你的: let element = $(this :selected);什么也没有发生。事实上,控制台没有像以前那样响应它没有发送任何获取。 PHPStorm 还向我显示该行有问题....怎么了?
      • @shibab 响应中没有发生任何事情。控制台仍然没有响应,因为它没有发送任何 GET 请求。
      猜你喜欢
      • 1970-01-01
      • 2022-07-07
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      相关资源
      最近更新 更多