繁体   English   中英

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

[英]How to add dropdown-menu based on other dropdown-menu using Ajax call in Laravel?

我有两个下拉字段,它们基于另一个Class&Section。 我想要

Select * from sections where class_id=selected Class Id.

我使用java脚本来管理它,但它不适合我。

我的下拉字段

     <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::get('/dropdown','DropDownController@index'); 
Route::get('classs/{id}/sections', 'DropDownController@getSection');

下拉控制器

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

课程模态

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

];

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

}

  }

部分模态

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

];

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

但实际上当我得到结果时,它会获得所有类和所有部分。 它可能在语法上有些错误。 我很困惑。 在这种情况下请帮助我。

首先需要确定是否直接调用了JS。

向脚本添加几个console.log调用:

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

现在检查:

  1. 是否正在listening for change event页面加载时记录的listening for change event ,并将正确的元素作为参数?
  2. change event fired是否在选择值更改时记录,具有正确的值?
  3. 是否使用正确的数据(标识,标题)记录了ajax response

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM