Laravel编辑表单,为下拉列表获取价值 [英] Laravel editing a form, getting value for dropdown lists

查看:62
本文介绍了Laravel编辑表单,为下拉列表获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户提交了一个表单,其中某些字段是下拉菜单,如

I had a user submit a form, and some of the fields were dropdowns like so

                <div class="form-group row">
                  <label for="type" class="col-md-3 col-form-label text-md-right">Type</label>
                    <div class="col-md-9">
                      <select class ="form-control" id="type" name="type">
                        <option>Apartment</option>
                        <option>House</option>
                        <option>Studio</option>
                        <option>Flat</option>
                      </select>
                      @if ($errors->has('type'))
                          <span class="invalid-feedback">
                              <strong>{{ $errors->first('type') }}</strong>
                          </span>
                      @endif
                    </div>
                </div>

那很好.

我并不是试图允许用户编辑特定表单.我可以通过为输入赋值并像这样调用数据来获得标题和照片等其他部分

I'm no trying to allow users to edit a particular form. I can get other sections like title and photo by assigning a value to the input and calling the data like this

<input id="Address" type="text" class="form-control" name="address" value="{{$Advert->address }}" required autofocus>

但是,当我尝试对选择选项执行类似操作时,什么也没有出现.这是编辑页面上的下拉菜单.

But when I attempt to do something similar on a select option, nothing appears. This is a dropdown on the edit page.

<div class="form-group row">
                          <label for="type" class="col-md-3 col-form-label text-md-right">Type</label>
                            <div class="col-md-9">
                              <select class ="form-control" id="type" name="type" value="{{$Advert->type}}">
                                <option></option>
                                <option>Apartment</option>
                                <option>House</option>
                                <option>Studio</option>
                                <option>Flat</option>
                              </select>
                              @if ($errors->has('type'))
                                  <span class="invalid-feedback">
                                      <strong>{{ $errors->first('type') }}</strong>
                                  </span>
                              @endif
                            </div>
                        </div>

推荐答案

您需要在option元素上使用selected属性,而不是直接将值分配给select:

You need to use selected attribute on your option element instead of assigning the value directly to select:

<select class ="form-control" id="type" name="type">
      <option value="">Select</option>
      <option {{ $Advert->type == 'Apartment' ? 'selected':'' }}>Apartment</option>
      <option {{ $Advert->type == 'House' ? 'selected':'' }}>House</option>
      <option {{ $Advert->type == 'Studio' ? 'selected':'' }}>Studio</option>
      <option {{ $Advert->type == 'Flat' ? 'selected':'' }}>Flat</option>
 </select>

但是我建议您使用 Laravel Collective Forms 可以更好地处理表单,元素

But I suggest you use Laravel Collective Forms to have a better handling of form and it's elements

这篇关于Laravel编辑表单,为下拉列表获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆