我的代码有什么错误我是 ajax 的新手
What is the error in my code I am new in ajjax
我想使用 ajjax 创建一个依赖下拉框 select。我找不到我的错误。请帮我找出我的错误。选择专业根据专业显示医生姓名。但我不能这样做。
这是我的 Route.php
Route::get('/admin/appointment/view/{id}', 'AppointmentController@appointmentView'); Route::get('/admin/appointment/speciality', 'AppointmentController@doctorView')->name('admin.appointment.speciality');
我的约会控制器
public function doctorView(Request $request)
{
$data=Doctor::select('doctor_name','doctor_specialty')->where('doctor_specialty',$request->speciality)->take(100)->get();
return response()->json($data);//then sent this data to ajax success
}
查看文件:
<div class="form-group">
<label for="inputState" class="col-form-label">Doctor Speciality</label>
<select name="speciality" id="speciality" class="form-control doctor_speciality" data-dependent="state">
<option value="">Select Speciality</option>
@foreach($specialityLists as $specialityList)
<option value="{{$specialityList->speciality}}">{{$specialityList->speciality}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="inputState" class="col-form-label">Doctor Name</label>
<select name="doctors_name" id="doctors_name" class="form-control doctor_name " >
<option value="0" selected="true">Choose Doctor Name</option>
</select>
</div>
脚本文件:
$(document).ready(function(){
$(document).on('change','.doctor_speciality',function(){
// console.log("hmm its change");
var speciality_id = $(this).val();
// console.log(speciality_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url: '{!!URL::to('/admin/appointment/speciality')!!}',
data:{'id':speciality_id},
success:function(data){
// console.log('success');
// console.log(data);
// console.log(data.length);
op+='<option value="0" selected disabled>Choose Doctor</option>';
for(var i=0;i<data.length;i++){
op+='<option value="'+data[i].doctor_specialty+'">'+data[i].doctor_name+'</option>';
}
div.find('.doctor_name').html(" ");
div.find('.doctor_name').append(op);
},
error:function(){
}
});
});
});
让我向你澄清....
使用 post
更改路线
Route::post('/admin/appointment/speciality', 'AppointmentController@doctorView')->name('admin.appointment.speciality');
现在你的ajax
部分
$(document).ready(function(){
$(document).on('change','.doctor_speciality',function(){
var speciality_id = $(this, 'option:selected').val();
$.ajax({
type:'post',
url: "{{ route('admin.appointment.speciality') }}",
data:{speciality:speciality_id,_token:@json(csrf_token())},
success:function(data){
// console.log(data);
var op ='<option value="0" selected disabled>Choose Doctor</option>';
$.each(data,function(key,value){
op+='<option value="'+value.doctor_specialty+'">'+value.doctor_name+'</option>';
});
$('.doctor_name').empty().append(op);
},
error:function(data){
console.log(data);
}
});
});
});
如果你和医生有一对多的关系table那么在你看来改变这个。
@foreach($specialityLists as $specialityList)
<option value="{{$specialityList->id}}">{{$specialityList->speciality}}</option>
@endforeach
然后在控制器中做这样的事情....
public function doctorView(Request $request)
{
//Here doc_spec_id will be your foreign key
//$request->speciality this will be your specialitylist primary key
return Doctor::select('doctor_name','doctor_specialty')->where('doc_spec_id',$request->speciality)->take(100)->get();
OR
//If $request->speciality is like a name mean to say string then write LIKE Query
return Doctor::select('doctor_name','doctor_specialty')->where('doctor_specialty','LIKE','%'.$request->speciality.'%')->take(100)->get();
}
如果数据是null
当然可以写成checks
等等...
我想使用 ajjax 创建一个依赖下拉框 select。我找不到我的错误。请帮我找出我的错误。选择专业根据专业显示医生姓名。但我不能这样做。 这是我的 Route.php
Route::get('/admin/appointment/view/{id}', 'AppointmentController@appointmentView'); Route::get('/admin/appointment/speciality', 'AppointmentController@doctorView')->name('admin.appointment.speciality');
我的约会控制器
public function doctorView(Request $request)
{
$data=Doctor::select('doctor_name','doctor_specialty')->where('doctor_specialty',$request->speciality)->take(100)->get();
return response()->json($data);//then sent this data to ajax success
}
查看文件:
<div class="form-group">
<label for="inputState" class="col-form-label">Doctor Speciality</label>
<select name="speciality" id="speciality" class="form-control doctor_speciality" data-dependent="state">
<option value="">Select Speciality</option>
@foreach($specialityLists as $specialityList)
<option value="{{$specialityList->speciality}}">{{$specialityList->speciality}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="inputState" class="col-form-label">Doctor Name</label>
<select name="doctors_name" id="doctors_name" class="form-control doctor_name " >
<option value="0" selected="true">Choose Doctor Name</option>
</select>
</div>
脚本文件:
$(document).ready(function(){
$(document).on('change','.doctor_speciality',function(){
// console.log("hmm its change");
var speciality_id = $(this).val();
// console.log(speciality_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url: '{!!URL::to('/admin/appointment/speciality')!!}',
data:{'id':speciality_id},
success:function(data){
// console.log('success');
// console.log(data);
// console.log(data.length);
op+='<option value="0" selected disabled>Choose Doctor</option>';
for(var i=0;i<data.length;i++){
op+='<option value="'+data[i].doctor_specialty+'">'+data[i].doctor_name+'</option>';
}
div.find('.doctor_name').html(" ");
div.find('.doctor_name').append(op);
},
error:function(){
}
});
});
});
让我向你澄清....
使用 post
Route::post('/admin/appointment/speciality', 'AppointmentController@doctorView')->name('admin.appointment.speciality');
现在你的ajax
部分
$(document).ready(function(){
$(document).on('change','.doctor_speciality',function(){
var speciality_id = $(this, 'option:selected').val();
$.ajax({
type:'post',
url: "{{ route('admin.appointment.speciality') }}",
data:{speciality:speciality_id,_token:@json(csrf_token())},
success:function(data){
// console.log(data);
var op ='<option value="0" selected disabled>Choose Doctor</option>';
$.each(data,function(key,value){
op+='<option value="'+value.doctor_specialty+'">'+value.doctor_name+'</option>';
});
$('.doctor_name').empty().append(op);
},
error:function(data){
console.log(data);
}
});
});
});
如果你和医生有一对多的关系table那么在你看来改变这个。
@foreach($specialityLists as $specialityList)
<option value="{{$specialityList->id}}">{{$specialityList->speciality}}</option>
@endforeach
然后在控制器中做这样的事情....
public function doctorView(Request $request)
{
//Here doc_spec_id will be your foreign key
//$request->speciality this will be your specialitylist primary key
return Doctor::select('doctor_name','doctor_specialty')->where('doc_spec_id',$request->speciality)->take(100)->get();
OR
//If $request->speciality is like a name mean to say string then write LIKE Query
return Doctor::select('doctor_name','doctor_specialty')->where('doctor_specialty','LIKE','%'.$request->speciality.'%')->take(100)->get();
}
如果数据是null
当然可以写成checks
等等...