【问题标题】:Select element dose not display selected value in it's input field - Laravel选择元素不在其输入字段中显示所选值 - Laravel
【发布时间】:2021-02-19 07:25:37
【问题描述】:

我正在 Laravel 中创建一个 crud 应用程序。我制作了编辑选项,当我单击编辑选项时,它会根据相应的 ID 显示来自数据库的数据。它会显示我在插入数据时输入的输入字段中的所有值,但选择元素不会在其输入字段中显示任何数据。

显示数据库中所有数据的页面

当我点击编辑时,它会显示如下图所示的编辑表单

您可以看到所有输入字段显示来自数据库的正确数据,但选择元素不显示任何值。

此表单的代码是

@foreach($result as $list)
    <div class="container">
        <!------------ Row 1 ------------>
        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label>NAME</label>
                    <input type="text"  name="name" class="form-control" value="{{ $list->cust_name }}">
                </div>
                @error('name')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>PHONE</label>
                    <input type="number" name="phone" class="form-control" value="{{ $list->cust_phone }}">
                </div>
                @error('phone')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>CITY</label>
                    <input type="text" name="city" class="form-control" value="{{ $list->cust_city }}">
                </div>
                @error('city')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>INSTALLER NAME</label>
                    <input type="text"  name="instaname" class="form-control" value="{{ $list->inst_name }}" >
                </div>
            </div>
        </div>
        <!------------ Row 2 ------------>
        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label>STORE NAME</label>
                    <select type="text" name="storename" class="form-control" value="{{ $list->storename }}"> 
                        <option> ~ SELECT STORE NAME ~</option>
                        <option>PACKAGES</option>
                        <option>EMPORIUM</option>
                        <option>FORTRESS</option>
                    </select>
                </div>
                @error('storename')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>ORDER DATE</label>
                    <input type="date"  name="odate" class="form-control" value="{{ $list->odate }}">
                </div>
                @error('odate')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>INSTALLATION DATE</label>
                    <input type="date"  name="instadate" class="form-control" value="{{ $list->inst_date }}">
                </div>
                @error('instadate')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>BRAND NAME</label>
                    <input type="text" name="bname" class="form-control" value="{{ $list->brandname }}">
                </div>
                @error('bname')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>                 
        </div>

        <!------------ Row 3 ------------>
        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label>PRODUCT NAME</label>
                    <input type="text" id="add" name="pname" class="form-control" value="{{ $list->cust_order }}"> 
                </div>
                @error('pname')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div> 
            <div class="col-md-3">
                <div class="form-group">
                    <label>ORDER STATUS</label>
                    <select name="ostatus" class="form-control" value="{{ $list->cust_order_status }}">
                        <option> ~ SELECT STATUS ~ </option>
                        <option>PROCESSED</option>
                        <option>COMPLETED</option>
                        <option>FAILED</option>
                        <option>CANCELLED</option>
                        <option>PENDING</option>
                    </select>
                </div>
                @error('ostatus')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>INSTALLATION DONE BY</label>
                    <select  name="doneby" class="form-control" value="{{ $list->inst_done_by }}">
                        <option> ~ DONE BY ~ </option>
                        <option>PCES</option>
                        <option>SELF</option>
                    </select>
                </div>
                @error('doneby')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>ADDITIONAL COMMENTS</label>
                    <input type="text" name="comments" class="form-control" value="{{ $list->comments }}">
                </div>
                @error('comments')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
        </div>

        <!------------ Row 4 ------------>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label>ADDRESS</label>
                    <input type="text" name="address" class="form-control" value="{{ $list->cust_address }}" >
                </div>
                @error('address')
                    <span id="field_error">{{$message}}</span>
                @enderror
            </div>
        </div>                     
    </div>
@endforeach

如何解决这个问题?

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    仅将值放入 select 不会使选项被选中。您必须在选项中添加selected 属性才能使其被选中。

    <select type="text" name="storename" class="form-control"> 
        <option> ~ SELECT STORE NAME ~</option>
        <option {{ $list->storename == 'PACKAGES' ? 'selected' : '' }}>PACKAGES</option>
        <option {{ $list->storename == 'EMPORIUM' ? 'selected' : '' }}>EMPORIUM</option>
        <option {{ $list->storename == 'FORTRESS' ? 'selected' : '' }}>FORTRESS</option>
    </select>
    

    同样适用于 ORDER STATUS 选择。但是我可以看到您没有为选项使用任何值。最好为每个选项使用一个值。考虑为每个选项使用唯一值。

    【讨论】:

      【解决方案2】:

      值没有显示在选择中,因为您在 select tag 中使用了值而不是 option tag 并在选项标签中添加关键字 selected

      <!-- The second value will be selected initially -->
      
      <select name="choice">
      <option value="first">First Value</option>
      <option value="second" selected>Second Value</option>
      <option value="third">Third Value</option>
      </select> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        相关资源
        最近更新 更多