【问题标题】:how to show selected values in multi-select如何在多选中显示选定的值
【发布时间】:2019-12-19 06:22:41
【问题描述】:

我面临一个关于多选的问题。 onchange 我正在运行 ajax 并且 on ajax 成功选项附加到多选。 但在编辑时,我无法触发 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');

            }
        });
    });

【问题讨论】:

    标签: laravel jquery-select2


    【解决方案1】:
     $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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2015-08-02
      • 2021-08-08
      • 2018-11-29
      相关资源
      最近更新 更多