duanhai1455 2017-11-10 08:32
浏览 67
已采纳

如何在编辑模式laravel中的下拉列表中选择默认值

In edit mode my selecte dropdown like this.

<select  name="comic_publisher" id="publishers" >
            @foreach($group as $team)
                <option value="{{$team['group_id']}}">{{$team['comic_group_name']}}</option>
            @endforeach
      </select>

and I want like if I edit the record the selected value associated with that record is by default here should be selected.

I am stuck with the little issue, can anyone please help me how to do that.

Thanks a lot.

  • 写回答

3条回答 默认 最新

  • dongqing904999 2017-11-10 08:46
    关注

    You need to retrieve first database inserted value, and then check that value with all options if match found you can select like,

    $db_selected_value = "2"; // retrieved data
    <select name="comic_publisher" id="publishers" >
            @foreach($group as $team)
                <option value="{{$team['group_id']}}" @if($db_selected_value == $team['group_id']) {{ 'selected' }} @endif>{{$team['comic_group_name']}}</option>
            @endforeach
      </select>
    

    here, @if($db_selected_value == $team['group_id']) {{ 'selected' }} @endif this line matches your old selected value to all the other. and selected selects

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?