laravel 如何在laravel 5中设置组合框选定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37017526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:43:09  来源:igfitidea点击:

How to set combobox selected value in laravel 5?

phplaravelcomboboxlaravel-5

提问by hendraspt

I am using laravel 5 and I have an update/edit form which include a combobox on it. But I can't set my combobox value depends on what in the database. Do you know how to do it?
Here is my controller:

我正在使用 laravel 5 并且我有一个更新/编辑表单,其中包含一个组合框。但是我不能设置我的组合框值取决于数据库中的内容。你知道怎么做吗?
这是我的控制器:

public function ubahsurat(Request $request)
{
    Surat::where('nomor_surat', '=', $request['nomor_surat'])->update(
                            ['id_jenis_surat' => $request['id_jenis_surat']],

    return redirect('/');
}

This is the view:

这是视图:

    <div class="col-sm-9">
         <select name="id_jenis_surat" class="form-control">
         <option></option>
            @foreach($jenis_surat as $js) 
            <option value="{{ $js->id_jenis_surat }}" > {{ $js->jenis_surat }} </option> 
            @endforeach
        </select>
   </div>

回答by Josh

All you need to do is set selected="selected"on the item which is active.

您需要做的就是selected="selected"在活动的项目上进行设置。

<option value="{{ $js->id_jenis_surat }}" {!! ($conditionForSelected ? "selected=\"selected\"" : "") !!}> {{ $js->jenis_surat }}</option>

Replace $conditionForSelectedwith whatever it is that determines if $js->id_jenis_suratis the current item that is selected.

替换$conditionForSelected为确定是否$js->id_jenis_surat选择当前项目的任何内容。

Your view is showing us the update code, not the view code.

您的视图向我们显示了更新代码,而不是视图代码。

回答by VipindasKS

You can do like this:

你可以这样做:

   <div class="col-sm-9">
     <select name="id_jenis_surat" class="form-control">
     <option></option>
        @foreach($jenis_surat as $js) 
        <option value="{{ $js->id_jenis_surat }}" @if($combobox_value_from_database==$js->id_jenis_surat) selected="selected" @endif> {{ $js->jenis_surat }} </option> 
        @endforeach
    </select>

Note: I would encourage you to use Laravel Form Builders. so you can avoid handling these type of issues.

注意:我鼓励您使用 Laravel 表单构建器。这样您就可以避免处理这些类型的问题。