[英]Laravel ajax returns undefined value
I have an dropdown that I want it to make dynamic. 我有一个下拉列表,希望它可以动态化。 In my example, a service has many categories and a categories belongs to a specific service.
在我的示例中,服务具有许多类别,并且类别属于特定服务。 When a dropdown service is selected.
选择下拉服务时。 It will display only all the categories that belongs to that service in the second dropdown.
在第二个下拉列表中,它将仅显示属于该服务的所有类别。
So here's what I have done so far. 所以这是我到目前为止所做的。
In my fields.blade.php 在我的fields.blade.php中
<!-- Service Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'Service:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control','required', 'id' => 'service_id'])!!}
</div>
<!-- Categories Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control','required', 'id' => 'category_id'])!!}
</div>
In my JS script 在我的JS脚本中
$(function() {
$('#service_id').change(function() {
var url = '{{ url('encoder') }}' + '/service/' + $(this).val() + '/categories/';
$.get(url, function(data) {
var select = $('form select[name= category_id]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.name + '</option>');
});
});
});
});
In my routes web.php 在我的路线上web.php
Route::group(['prefix' => 'encoder','namespace' => 'Encoder'], function () {
Route::get('service/{service}/categories', 'ServiceController@getCategories');
});
In my ServieController.php 在我的ServieController.php中
public function getCategories($idService)
{
if(!is_numeric($idService)) abort(404);
$service = Service::findOrFail($idService);
return $service->categories->pluck('name', 'id');
}
I think I am already fetching the right data because when a specific service has 2 categories. 我认为我已经在获取正确的数据,因为当特定服务具有2个类别时。 It returns me also 2, but the value is undefined in the dropdown.
它也返回2,但该值在下拉列表中未定义。
So I think the problem only here is that the script returns me un undefined value. 因此,我认为问题仅在于此脚本返回了我不确定的值。
Appreciate if someone could help. 感谢有人可以帮忙。 Thanks in advance.
提前致谢。
Change 更改
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.name + '</option>');
});
To 至
$.each(data,function(key, value) {
select.append('<option value=' + key + '>' + value + '</option>');
});
I doubt your js onchange event is working or not because, 我怀疑您的js onchange事件是否有效,因为,
You add ajax call on "onChange" event of dropdown using id attibute of dropdown 您使用下拉列表的ID样式在下拉列表的“ onChange”事件上添加ajax调用
$('#service_id').change(function() {
And your view file has dropdown using below code 并且您的视图文件具有使用以下代码的下拉列表
{!! Form::select('service_id', $services, null, ['class' => 'form-control','required'])!!}
So, in this declaration there is no "id" attribute specified. 因此,在此声明中未指定“ id”属性。
You should use 你应该用
{!! Form::select('service_id', $services, null, ['class' => 'form-control','required', 'id' => 'service_id'])!!}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.