根据下拉选择显示数据不起作用

时间:2019-04-10 05:08:55

标签: jquery laravel

我有类别表和俱乐部表,两者之间的关系是

  • 一个类别有很多俱乐部
  • 1个类别可以有很多俱乐部
  • 1个俱乐部有1个类别

我想根据类别显示俱乐部,将通过下拉列表选择俱乐部,因此其他俱乐部将被隐藏。请参阅屏幕截图以了解更多enter image description here

  1. 我的控制器(将类别名称添加到下拉列表没有问题)
    public function club()
    {
        $categories_name = Category::pluck('category_name','id'); 
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        $data = array(
            'user_clubs' => $user->clubs,
            'categories_name' => $categories_name
        );
        return view('pages.dashboard.club_dashboard')->with($data);
    }
  1. 我的看法
<div class="form-group">
        <strong>{{Form::label('categories_name', 'Select Category')}}</strong>
        {{Form::select('categories_name', $categories_name, null, ['class' => 'form-control'], array('name'=>'categories_name[]'))}}
</div>

@if (count($user_clubs)>0)
<table class="table table-striped">
    <tr>

        <th><strong>Club Name</strong></th>
        <th></th>
        <th></th>
    </tr>
    @foreach ($user_clubs as $club)
    <tr id="{{$club->category->id}}">
        <th>{{$club->club_name}} | {{$club->category->category_name}} </th>
        <td><a href="/clubs-soc/lists/{{$club->id}}/edit" class="btn btn-warning">Edit</a></td>
        <td>
            {!!Form::open(['action' => ['ClubsController@destroy', $club->id], 'method' => 'POST', 'class' => 'float-right'])!!} 
            {{Form::hidden('_method','DELETE')}} 
                {{Form::submit('Delete', ['class' => 'btn btn-danger'])}} 
            {!!Form::close()!!}
        </td>
    </tr>
    @endforeach
</table>
@else
<p>You Have No posts</p>
@endif

3。类别模型

class Category extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }

    public function clubs(){
        return $this->hasMany('App\Club');
    }
}

4。俱乐部模型

class Club extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }

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

5。我的脚本 我是根据朋友给出的答案写的这个脚本,仍然无法正常工作,请帮我找到:

  • 我在哪里弄错了?
  • 我应该将脚本放在哪里?
<script>
    $(document).ready(function(){
        $("#categories_name").on("change", function(){
            var category_id = $(this).val();
            $("table tr").each(function(index){
                if (index != 0) {
                    $row = $(this);
                    if ($row.attr('id')!=category_id) {
                        $row.hide();
                    }
                    else {
                        $row.show();
                    }
                }
            });
        });
    });​
</script>

0 个答案:

没有答案