【问题标题】:What is the error in my code I am new in ajjax我的代码中有什么错误我是 ajax 新手
【发布时间】:2020-02-19 07:52:57
【问题描述】:

我想使用 ajax 创建一个依赖下拉选择框。我找不到我的错误。请帮我找出我的错误。选择专科根据专科显示医生姓名。但我不能这样做。 这是我的 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(){
                }

            });
    });

});

【问题讨论】:

    标签: ajax laravel-6


    【解决方案1】:

    让我澄清一下......

    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);
                }
    
            });
       });
    
    });
    

    如果您与医生表有一对多关系,那么在您看来更改此设置。

       @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等等...

    【讨论】:

    • 我已经更新了我的代码,看看有什么不同。但是你面临什么错误?
    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 2015-05-10
    • 2022-10-16
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多