繁体   English   中英

我正在尝试使用 ajax 调用获取特定产品的价格。 到目前为止,我已经尝试过这个。 但我在选择产品后没有得到价格

[英]I am trying get price of specific product using ajax call. so far i had tried this. but i am not getting the price after selecting product

我的blade.php

<select name="productname[]" class="form-control productname">
   <option value="0" selected="true" disabled="true">Select Product</option>
 @foreach ($stock as $product)
   <option value="{{$product->id}}">{{$product->productname}}</option>
 @endforeach
 </select>

用户从这里选择产品,我想要自动填充 ptice 输入字段。

<td><input type="text" name="price[]" class="form-control price"></td>

使用这个 javascript

 $('tbody').delegate('.productname','change', function(){
  var tr=$(this).parent().parent();
  var id = tr.find('.productname').val();
  var dataId={'id':id};
   $.ajax({
    type    :   'GET',
    url     : "{{route('findprice')}}",
    dataType:   'json',
    data    :   dataId,
    success:function(data){
        tr.find('text.price').val(data[0].price);
    }
   });
 });

在我的控制器中我正在使用这个东西。

public function findprice(Request $request)
  {
   $data = Stock::where('price')->where('id',$request->id)->first();
   return response()->json($data);
  }

任何可以帮助我的人。

你有一个查询错误。 添加这个

$data = Stock::select('price')
           ->where('id',$request->id)
           ->first();

您放置了“where”而不是“select”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM