如何隐藏下拉列表以及如何从下拉列表中进行多选?

How to hide dropdown and how to multiselect from dropdown?

我有工作日的下拉列表,我想 select 多天,比如 'Monday' 和 'Tuesday' 我有几个问题

这是我的看法

        <div class="form-group">
            {{Form::label('weekdays', 'Week Days')}}

            {{Form::select('weekdays',$weekdays,null,array('name'=>'weekdays[]', 'multiple' => 'multiple'))}}
            {{Form::select('timetable',$time,null,array('name'=>'time[]'))}}
        </div>

控制器

        $categories_name = Category::pluck('category_name','id');    
        $data = array(
            'weekdays' => [
                'Monday',
                'Tuesday',
                'Wednesday'
            ],
            'time' => [
                '2p.m',
                '3p.m'
            ],
            'categories_name' => $categories_name
        );
        return view('pages.clubs.create_club')->with($data);
    }

see the screenshot to understand what i mean

对于 1 和 2,您可以使用名为 chosen 的插件,它有详细的文档并且很容易实现。

对于第 3 题,您可以将其以 json 格式保存在数据库中,如下所示:

$days = "Monday,Tuesday,Wednesday"; 
//or $days = $request->weekdays;
$parsed = explode(",", $days);
$selected_weekdays = json_encode($parsed);

Model::create(['weekdays' => $selected_weekdays]);

然后您可以通过像这样解码 json 字符串来显示 blade 上的值:

$selected_weekdays = json_decode($model->weekdays);

{{Form::select('weekdays[]',$weekdays,$selected_weekdays,array('multiple' => 'multiple'))}}

更新 1:

建议使用 bower 安装:

bower install chosen

要使用所选:

<link rel='stylesheet' type="text/css" href="{{ asset('bower_components/chosen/chosen.min.css') }}" rel="stylesheet">

{{Form::select('weekdays[]',$weekdays,$selected_weekdays,array('multiple' => 'multiple', 'class' => 'chosen-select'))}}

<script src="{{ asset('bower_components/chosen/chosen.jquery.min.js') }}"></script>

<script>
    $(".chosen-select").chosen();
</script>