在 Laravel 中获取编辑表单上的选定选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45268471/
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 16:22:00  来源:igfitidea点击:

Getting selected option on edit form in laravel

phpformslaravel

提问by Robert Nicjoo

I have an editable form for my website orders and I have these fields:

我的网站订单有一个可编辑的表单,我有以下字段:

Userquantitynotestatus

Userquantitynotestatus

I also have other options in this form, but only these fields are important to me in order to be able to get the default values.

我在此表单中还有其他选项,但只有这些字段对我来说很重要,以便能够获得默认值。

For example, I want to be able to see the amount of quantity that a user has ordered by default and then I could change it or leave it alone. Currently all my drop-down values start from the first value and not what the user has chosen.

例如,我希望能够查看用户默认订购的数量,然后我可以更改它或不理会它。目前我所有的下拉值都从第一个值开始,而不是用户选择的值。

How can I do that?

我怎样才能做到这一点?

This is my form:

这是我的表格:

{{ Form::model($order, array('route' => array('orders.update', $order->id), 'method' => 'PUT', 'files' => true)) }}

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Order ID</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-word-o"></i></span>
                      <input class="form-control" type="text" name="" value="{{ $order->id }}" readonly>
                    </div>
                  </div>
                </div>


                <div class="form-group">
                  <label class="col-md-3" for="invoice_nu">Invoice Number:</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-sort-numeric-asc"></i></span>
                      <input class="form-control" type="text" name="invoice_nu" value="{{ $order->invoice_nu }}" readonly>
                    </div>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">User</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-user-o"></i></span>
                      <select class="form-control" id="type" name="user_id">
                        @foreach($users as $user)
                            <option value="{{ $user->id }}">{{ $user->name }}</option>
                        @endforeach
                      </select>
                    </div>
                  </div>
                </div>



                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Quantity</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
                      <select class="form-control" id="type" name="quantity">
                        <option value="">Select Quantity</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                      </select>
                    </div>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Note</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-word-o"></i></span>
                      <textarea name="name" class="form-control" rows="8">@if(!empty($order->note)){{ $order->note }}@else-@endif</textarea>
                    </div>
                  </div>
                </div>


                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Status</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
                      <select class="form-control" id="type" name="status">
                        <option value="Waiting Payment">Waiting Payment</option>
                        <option value="Paid">Paid</option>
                      </select>
                    </div>
                  </div>
                </div>


                {{ Form::submit('Save', array('class' => 'btn btn-primary mt-20')) }}

                {{ Form::close() }}

回答by bhill77

you can compare $order->quantitywith option value to add selectedattribute

您可以$order->quantity与选项值进行比较以添加selected属性

<select class="form-control" id="type" name="quantity">
    <option value="">Select Quantity</option>
    <option value="1" {{ $order->quantity == 1 ? 'selected' : '' }}>1</option>
    <option value="2" {{ $order->quantity == 2 ? 'selected' : '' }}>2</option>
    <option value="3" {{ $order->quantity == 3 ? 'selected' : '' }}>3</option>
    <option value="4" {{ $order->quantity == 4 ? 'selected' : '' }}>4</option>
    <option value="5" {{ $order->quantity == 5 ? 'selected' : '' }}>5</option>
</select>

for user compare $user->idwith $order->user_id(or something else according your data)

用户比较$user->id$order->user_id(或别的东西,根据您的数据)

<select class="form-control" id="type" name="user_id">
    @foreach($users as $user)
        <option value="{{ $user->id }}" {{ $user->id == $order->user_id ? 'selected' : '' }}>{{ $user->name }}</option>
    @endforeach
</select>

回答by Svetoslav Dimitrov

Check if you have quatity and then add selected attribute to respective option.

检查您是否有 quatity,然后将选定的属性添加到相应的选项。

        <select class="form-control" id="type" name="quantity">
            @if($order->quantity)
            <option value="{{$order->quantity}}" selected>{{$order->quantity}}</option>
            @else
            <option value="">Select Quantity</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            @endif
        </select>