使用jQuery AJAX 选择依赖的下拉列表并显示所选值

ufj5ltwl  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(95)

x1c 0d1x我想单独获取每个lab_test的成本,但它给了我所有的成本。我尝试使用->first(); and ->first(['cost']);在控制器中,但得到类似“响应内容必须是实现__toString()的字符串或对象”的错误,对象“给定”。我正在我的项目中实现依赖下拉选择框。
Controller Test.php

class Test extends Controller
{
          public function cost(Request $request){
            $lab_data = \DB::table('lab_category')->select('lab_category_id','category_name')->get();
            return view('pages/medicinecost')->with('lab_category',$lab_data);
        }

        public function costSub(Request $request){
            $get_lab_cat_id = $request->get('labCategId');
            $lab_sub_data = \DB::table('lab_sub_category')
                ->leftJoin('lab_category','lab_category.lab_category_id','=','lab_sub_category.category_id')
                ->where('lab_category_id', $get_lab_cat_id)
                ->select('sub_category_name','lab_sub_category_id')->get();
            return $lab_sub_data;
        }

        public function costTest(Request $request){
            $get_lab_sub_id = $request->get('labSubId');
            $lab_test_data = \DB::table('lab_test')
                ->leftJoin('lab_sub_category','lab_sub_category.lab_sub_category_id'
                    ,'=','lab_test.sub_category_id')
                ->where('sub_category_id',$get_lab_sub_id)
                ->where('hide',0)
                ->select('lab_name','lab_test_id')->get();
            return $lab_test_data;
        }

        public function labTestprice(Request $request){
            $get_lab_test_id = $request->get('labtid');
            $cost = \DB::table('lab_test')
                ->leftJoin('lab_sub_category','lab_sub_category.lab_sub_category_id','=','lab_test.sub_category_id')
                ->where('lab_test_id',$get_lab_test_id)
                ->orWhere('hide',0)
                ->select('cost','lab_test_id')->get();
            //  dd($cost);
            return $cost;
        }
}

字符串
Route.php

Route::get('labdetails','Test@cost');
Route::post('get_lab_sub','Test@costSub');
Route::post('get_lab_sub_cat','Test@costTest');
Route::post('get_lab_cost_rs','Test@labTestprice');


medicinecost.blade.php

<body>
    <h1><p>Lab Cost</p></h1><br>
    <div class="container">
        <div class="col-lg-3">
            <div class="form-group">
                <select name="labCat" id="labC" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Category</option>
                        @if(isset($lab_category))
                            @foreach($lab_category as $lb)
                                <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
                            @endforeach
                        @endif
                </select>
            </div>
            <div class="form-group">
                <select name="labSub" id="labS" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Sub Category</option>
                </select>
            </div>
            <div class="form-group">
                <select name="labTest" id="labT" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Test</option>
                </select>
            </div>
            <div class="col-md-2"><span id="loader"><i class="fa fa-spinner fa-3x fa-spin"></i></span></div>
        </div>

    </div>
    <div>
        <p id="testCost"></p>
    </div>
    <script>

    $(document).ready(function() {

        $('#labC').on('change', function(){
            var labCategId = $(this).val();
            if(labCategId) {
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url: 'get_lab_sub',
                    type:"POST",
                    data : {labCategId:labCategId,"_token":"{{ csrf_token() }}"},
                    dataType:"json",

                    success:function(data) {
                        if(data){
                            $('#labS').empty();

                            $.each(data, function(key, value){
                                $('#labS').append('<option value="'+value.lab_sub_category_id+'">' + value.sub_category_name + '</option>');

                            });
                        }

                    },

                });
            } else {
                $('select[name="labS"]').empty();
            }

        });

        $('#labS').on('change' ,function () {
            var labSubId = $(this).val();
                if(labSubId){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url :'get_lab_sub_cat',
                    type:"POST",
                    data :{ labSubId:labSubId,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $('#labT').empty();
                            $.each(data, function(key, value){

                                $('#labT').append('<option value="'+ value.sub_category_id +'">' + value.lab_name + '</option>');

                            });
                        }
                        else {
                            $('#labT').empty();
                        }
                    }
                });
            }

        })
       $('#labT').on('change' ,function () {
            var lab_test_cost_id = $(this).val();
              if(lab_test_cost_id){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url :'get_lab_cost_rs',
                    type:"POST",
                    data :{ labtid:lab_test_cost_id,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            //$('#labT').empty();
                            $.each(data, function(key, value){
                                $('#testCost').append('<p value="'+value.lab_test_id +'"> '+ value.cost + '</p>');
                                //$('#testCost').append('<p value="'+value.lab_test_id+'">'+value.cost+'</p>');
                            });
                        }
                    }
                });
            }
        });

    });
    </script>
    </body>


每当我从第三个下拉列表中选择lab_test时,它应该显示该perticuler lab_tests的成本,但不是显示所有成本。

zbwhf8kr

zbwhf8kr1#

试试这个:

<body>
    <h1><p>Lab Cost</p></h1><br>
    <div class="container">
        <div class="col-lg-3">
            <div class="form-group">
                <select name="labCat" id="labC" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Category</option>
                        @if(isset($lab_category))
                            @foreach($lab_category as $lb)
                                <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
                            @endforeach
                        @endif
                </select>
            </div>
            <div class="form-group">
                <select name="labSub" id="labS" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Sub Category</option>
                </select>
            </div>
            <div class="form-group">
                <select name="labTest" id="labT" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Test</option>
                </select>
            </div>
            <div class="col-md-2"><span id="loader"><i class="fa fa-spinner fa-3x fa-spin"></i></span></div>
        </div>

    </div>
    <div>
        <p id="testCost"></p>
    </div>
    <script>

    $(document).ready(function() {

        $('#labC').on('change', function(){
            var labCategId = $(this).val();
            $('#labS').html('');
            $('#labS').append('<option value="0" disabled="true" selected="true">Select Lab Sub Category</option>');
            $('#labT').html('');
            $('#labT').append('<option value="0" disabled="true" selected="true">Select Lab Test</option>');
            if(labCategId) {
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url: 'get_lab_sub',
                    type:"POST",
                    data : {labCategId:labCategId,"_token":"{{ csrf_token() }}"},
                    dataType:"json",

                    success:function(data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#labS').append('<option value="'+value.lab_sub_category_id+'">' + value.sub_category_name + '</option>');

                            });
                        }else {
                            $('#labS').empty();
                        }

                    },

                });
            } else {
                $('select[name="labS"]').empty();
            }

        });

        $('#labS').on('change' ,function () {
            var labSubId = $(this).val();
           $('#labT').html('');
           $('#labT').append('<option value="0" disabled="true" selected="true">Select Lab Test</option>');

            if(labSubId){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    depends : ['#labC'],
                    url :'get_lab_sub_cat',
                    type:"POST",
                    data :{ labSubId:labSubId,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#labT').append('<option value="'+ value.lab_test_id +'">' + value.lab_name + '</option>');
                            });
                        }
                        else {
                            $('#labT').empty();
                        }
                    }
                });
            }

        })
       $('#labT').on('change' ,function () {
            var lab_test_cost_id = $(this).val();
            if(lab_test_cost_id){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',

                    url :'get_lab_cost_rs',
                    type:"POST",
                    data :{ labtid:lab_test_cost_id,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#testCost').append('<h5>The Lab Cost is </h5><p value="'+value.lab_test_id +'"> '+ value.cost + '</p>');
                            });
                        }
                    }
                });
            }
        });

    });
    </script>
    </body>

字符串

ee7vknir

ee7vknir2#

从属下拉菜单编辑

$(document).ready(function(){
$('table').on('click','#edit',function(){
var id = $(this).parents("tr").attr("id");
alert(id);

if(confirm('Are you sure to edit this record ?'))
{ 
    $.ajax({
       url: 'edit.php',
       type: 'GET',
       data: {id: id},
       error: function() {
          alert('Something is wrong');
       },
       success: function(response) {
         $('#submit').hide();
         $('#update').css('display','block');
         
        var data=JSON.parse(response);
        for(i in data){
          alert(data[i].id);
           $('#id').val(data[i].id);
           $('#name').val(data[i].name);
           $('#age').val(data[i].age);
           $('#phone').val(data[i].phone);
           $('#email').val(data[i].email);
           $('#state').val(data[i].state);
           $.ajax({
          url:'drop.php',
          type:'post',
          data:{s_id:data[i].state},
          success:function(res){
            $('#district').html(res);
        
           $('#district').val(data[i].district);
           $('#address').val(data[i].address);
           $('#gender').val(data[i].gender);

          }
         })
        }
        
    }
    });

   }

   })
   })
   </script>

字符串

相关问题