【问题标题】:Laravel editing a form, getting value for dropdown listsLaravel 编辑表单,获取下拉列表的值
【发布时间】:2018-08-05 01:35:25
【问题描述】:

我让一个用户提交了一个表单,其中一些字段是这样的下拉列表

                <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>

效果很好。

我不想让用户编辑特定的表单。我可以通过为输入分配一个值并像这样调用数据来获取标题和照片等其他部分

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

但是当我尝试在选择选项上做类似的事情时,什么也没有出现。这是编辑页面上的下拉菜单。

<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>

【问题讨论】:

    标签: laravel laravel-5 drop-down-menu blade


    【解决方案1】:

    您需要在option 元素上使用selected 属性,而不是将值直接分配给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 更好地处理表单及其元素

    【讨论】:

    • 非常感谢
    • 当这些值没有被硬编码时,我将如何做到这一点。我试过 foreach ($Advert->county as $county) endforeach
    • 我没有关注
    • 而不是将其应用于硬代码值。我如何将其应用于从数据库中提取的数据
    • 您需要一个所有可选值的列表,并且需要一个选定值,然后在列表上执行 foreach 并将列表中的每个值与单个选定值进行比较
    【解决方案2】:

    就像在我使用 Laravel 集体表单之前建议的那样;像这样:

    {!! Form::select('type',$selectOptions,null,['id' =>'type','class' => 'form-control'])!!}
    

    变量 $selectOptions 将包含所有选项:

    $selectOptions = [
        '' => 'Select',
        'Apartment' => 'Apartment',
        'House' => 'House',
        'Studio' => 'Studio',
        'Flat' => 'Flat'
    ];
    

    这很容易做到。当然,一切都应该在一个表单中。

    {!! Form::open() !!}} or {!! Form::model($model) !!}
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2020-05-08
      • 2021-09-12
      相关资源
      最近更新 更多