如何在多选中显示所选值

时间:2019-12-19 06:22:41

标签: laravel jquery-select2

我面临一个有关多重选择的问题。 onchange我是runnig ajax,并且在ajax上,成功选项将追加到multiselect。 但是在编辑时,我无法触发Ajax,也无法在多选中显示选定的选项。

请检查我的代码

逃跑

<div class="form-group {{ $errors->first('preferred_city', 'has-error') }}">
                                        <label class="control-label  col-lg-2">Preferred City
                                            : </label>

                                        <div class="col-lg-10">

                                            <select name="preferred_city[]" class="form-control" id="preferred_city" multiple="multiple" style="color: #333">
                                                @foreach($cities as $preferred_city)
                                                    @if(!empty($studentsDetails->preferred_city))
                                                        <option value="{{$preferred_city->id}}" {{ (in_array($preferred_city->id,\GuzzleHttp\json_decode($studentsDetails->preferred_city) )) ? 'selected' : '' }}>{{$preferred_city->city_name}}</option>
                                                    @else
                                                        <option value="{{$preferred_city->id}}">{{$preferred_city->city_name}}</option>
                                                        @endif

                                                @endforeach
                                            </select>
                                            <span class="help-block">{{ $errors->first('preferred_city', ':message') }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group {{ $errors->first('collage_preferences', 'has-error') }}">
                                        <label class="control-label  col-lg-2">College Preferences

                                            : </label>

                                        <div class="col-lg-10">
                                            <select id="collage_preferences" name="collage_preferences[]" class="form-control" multiple="multiple">

                                            </select>
                                            <span class="help-block">{{ $errors->first('collage_preferences', ':message') }}</span>
                                        </div>
                                    </div>

js

我是从数据库中获取拼贴ID的

 var collage_pref = {!! $studentsDetails->collage_preferences !!}

我在页面加载时触发preferred_city

 $(document).ready(function(){
        if(studentsDetails !=null){
            $('#preferred_city').trigger('change');

// $('#pic_file')。trigger('change');             }

    });


 $('#preferred_city').on('change',function(){
        $("#collage_preferences").multiselect('rebuild');

        $.ajax({
           url:'{{route('student.getInstituteslists')}}',
            type:'get',
            async: false,
            data:{grad_course:$('#grad_course').val(),preferred_city:$('#preferred_city').val()},
            success:function(e){

                $('#collage_preferences').multiselect('refresh');
                e.forEach(function(inst){
                    var data = '';


                    data +='<option value="'+ inst.id+'" >'+ inst.institute_name+'</option>';


                    $('#collage_preferences').append(data);

                });
                $('.collage_preferences').multiselect('rebuild');

            }
        });
    });

1 个答案:

答案 0 :(得分:0)

 $data = [];
$data['airlines'] = '';
$airlines = (Airline::select('Code','Name')->get())->toArray();
$myairlines = BlackoutAirline::where('SupplierId',$request->SupplierId)->pluck('AirlineCode')->toArray();

foreach($airlines as $airline) {
                if(in_array($airline['Code'], $myairlines) ){

                    $data['airlines'] .= '<option value="'.$airline['Code'].'" selected>'.$airline['Name'].'</option>';
                }
                else{
                    $data['airlines'] .= '<option value="'.$airline['Code'].'">'.$airline['Name'].'</option>';
                }

        }
        return json_encode($data);