【问题标题】:How to add dropdown-menu based on other dropdown-menu using Ajax call in Laravel?如何在 Laravel 中使用 Ajax 调用基于其他下拉菜单添加下拉菜单?
【发布时间】:2019-04-09 16:13:27
【问题描述】:

我有两个基于另一个类和部分的下拉字段。 我想要

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

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

【问题讨论】:

  • 您的输入名为class,但在JS 中您正在寻找name=classs
  • 你是在说
  • 没错。您可以在change 监听器中添加console.log 以查看它是否被调用。
  • 我改变了它,但它对我不起作用..
  • 你能详细说明一下我在哪里可以添加console.log

标签: javascript php jquery ajax laravel


【解决方案1】:

您首先需要确定您的 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 是否已登录页面加载,并使用正确的元素作为参数?
  2. change event fired 登录的选择值是否更改,值是否正确?
  3. ajax response 记录的数据是否正确(ID、标题)?

【讨论】:

  • 调用时只显示白页。
  • 我丢失了现在页面加载但没有控制台消息显示。
  • 这很奇怪。是否还有其他内容被记录?尝试在jQuery(document) 之前添加console.log(jQuery); 以查看是否实际加载了jQuery。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多