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



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

使用此脚本

$('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();

您放置了"位置"而不是"选择">

相关内容

最新更新