我想根据用户从同一页面上的下拉列表中选择的内容自动填充页面中的一些文本框。 因此,如果用户更改下拉列表选择,则文本框也会根据所选的下拉列表更改值。
这是我的代码..
我的控制器是 ProductController.php
public function getPrice()
{
$getPrice = $_GET['id'];
$price = Product::all()->where('id', $getPrice)->get();
return Response::json($price);
}
溃败
Route::get('getPrice/{id}', 'ProductController@getPrice');
查看blade.php是
<table><tr><td>Product</td><td>Qty</td><td>Price</td><td></td></tr>
<tr><td>
<select name="productC" id="productC" class="form-control product">
@foreach($products as $product)
<option value="{{ $product->name }}" id="{{ $product->id }}" class="productC custom-select">
{{ $product->name }}
</option>
@endforeach
</select>
</td>
<td width="20%">
<input type="number" id="qty" min="0" value="0" class="form-control">
</td>
<td>
<input type="text" id="price" />
</td>
<td align="right"><input type="submit" class="btn btn-success" name="" value="Add"></td></tr>
</table>
Ajax 代码
<script>
$(document).ready(function(){
$('#productC').change(function() {
var pid = $(this).find(':selected')[0].id;
$.ajax({
type:'GET',
url:'getPrice/{id}',
data:{id:pid},
dataType:'json',
success:function(data)
{
$.each(data, function(key, resp)
{
$('#price').text(resp.price);
});
}
});
});
</script>
答案 0 :(得分:0)
使用以下内容:: $('#price').val(resp.price);