普通网友 2019-04-09 16:13
浏览 77

如何在Laravel中使用Ajax调用基于其他下拉菜单添加下拉菜单?

I'm having two dropdown fields which is based on one another Class & Section. I want

Select * from sections where class_id=selected Class Id.

I used java script to manage that but it is not working for me.

My dropdown fields

     <div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="col-md-6">
                <label>Category</label>
                <select class="form-control" name = "class">
                    <option value="0">Please Select Category</option>
                    @foreach($classses as $classs)
                        <option value="{{$classs->id}}">{{$classs->title}}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div>
            <div class="col-md-6">
                <label>Products</label>
                <select class="form-control" name = "section">
                    <option value="0">Please Select Product</option>
                    @foreach($sections as $section)
                        <option value="{{$section->id}}">{{$section->title}}</option>
                    @endforeach

                </select>
            </div>
        </div>
    </div>
</div>

JavaScript

<script>
jQuery(document).ready(function ($) {
    $('select[name=classs]').on('change', function () {
        var selected = $(this).find(":selected").attr('value');
        $.ajax({
            url: base_url + '/classs/'+selected+'/sections/',
            type: 'GET',
            dataType: 'json',

        }).done(function (data) {

            var select = $('select[name=section]');
            select.empty();
            select.append('<option value="0" >Please Select Product</option>');
            $.each(data,function(key, value) {
                select.append('<option value=' + key.id + '>' + value.title + '</option>');
            });
            console.log("success");
        })
    });
});

Route

Route::get('/dropdown','DropDownController@index'); 
Route::get('classs/{id}/sections', 'DropDownController@getSection');

Dropdown Controller

 public function index(){

    $classses = Classs::all();
    $sections =Section::all();

    return view('classs.a', compact('classses','sections'));
}



   public function getSection($id){
    if($id!=0){

        $sections = Classs::find($id)->sections()->select('id', 'title')->get()->toArray();
    }else{
        $sections = Section::all()->toArray();
    }
    return response()->json($sections);
}
}

Classs Modal

  class Classs extends Model
{
//
    protected $fillable = [
    'id',
    'title',

];

public function section()
{
    return $this->hasMany('App\Section');

}

  }

Section Modal

          class Section extends Model
{
//
protected $fillable = [
    'id',
    'title',
    'class_id',  //foreign key of Classs
    'user_id',

];

public function classs()
{
    return $this->belongsTo('App\Classs');
}

But in actual when I get result, it gets all the classes & all the sections. It maybe some error in syntax. I m so stuck. Please help me in this case.

  • 写回答

1条回答 默认 最新

  • dpf71390 2019-04-09 17:19
    关注

    You first need to make sure if your JS is getting called directly.

    Add a couple of console.log calls to the script:

    <script>
    jQuery(document).ready(function ($) {
        console.log('listening for change event', $('select[name=classs]')[0]);
    
        $('select[name=classs]').on('change', function () {
            var selected = $(this).find(":selected").attr('value');
            console.log('change event fired. selected value:', selected);
            $.ajax({
                url: base_url + '/classs/'+selected+'/sections/',
                type: 'GET',
                dataType: 'json',
    
            }).done(function (data) {
                console.log('ajax response', data);
    
                var select = $('select[name=section]');
                select.empty();
                select.append('<option value="0" >Please Select Product</option>');
                $.each(data,function(key, value) {
                    select.append('<option value=' + key.id + '>' + value.title + '</option>');
                });
                console.log("success");
            })
        });
    });
    

    Now check:

    1. Is listening for change event logged on page load, with the correct element as a parameter?
    2. Is change event fired logged on select value change, with the correct value?
    3. Is ajax response logged with the correct data (id, title)?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?