【问题标题】:Get selected value from select in Laravel Blade从 Laravel Blade 中的选择中获取选定的值
【发布时间】:2021-03-11 21:22:10
【问题描述】:

如何在 select 中选择客户端以检查其“irpf”字段是否为空?

我尝试检查循环的每次迭代,但输入字段重复

<form method="POST" action="{{ route('viewNuevaFacturaNegociador') }}" enctype="multipart/form-data">
                    @csrf

                    <div class="form-group">
                        <label for="tipo_cliente">Nombre del cliente</label>
                        <select class="form-control" name="id_cliente" required>
                            @foreach($users_client as $client)
                            <option value="{{ Crypt::encrypt($client->id) }}" >{{ $client->name }}</option>
                            @endforeach
                        </select>
                    </div>

                    @if($client->irpf != null)
                    <label for="tipo_cliente">IRPF</label>
                    <input type="text" value="{{$client->irpf}}" class="form-control" disabled/>
                    @else
                    <label for="tipo_cliente">IRPF</label>
                    <input type="text" name="irpf" value="{{ old('irpf') }}" class="form-control" placeholder="Introduce el porcentaje de IRPF (sin el %)" maxlength="2" pattern="[0-9]+" required/>
                    @endif



                    <div class="text-right">
                        <input type="hidden" name="id_client" value="">
                        <button type="submit" class="btn btn-success waves-effect waves-light">Create invoice</button>
                    </div>

                </form>

【问题讨论】:

  • 你能分享你的控制器功能吗

标签: laravel laravel-blade


【解决方案1】:

您只需将其放入foreach()

<form method="POST" action="{{ route('viewNuevaFacturaNegociador') }}" enctype="multipart/form-data">
    @csrf

    <div class="form-group">
        <label for="tipo_cliente">Nombre del cliente</label>
        <select class="form-control" name="id_cliente" required>
        @foreach($users_client as $client)
            <option value="{{ Crypt::encrypt($client->id) }}" >{{ $client->name }}</option>
        @endforeach
        </select>
    </div>

    @foreach($users_client as $client)
        @if(!is_null($client->irpf))
            <label for="tipo_cliente-{{$loop->iteration}}">IRPF</label>
            <input id="tipo_cliente-{{$loop->iteration}}" type="text" value="{{$client->irpf}}" class="form-control" disabled/>
        @else
            <label for="tipo_cliente-{{$loop->iteration}}">IRPF</label>
            <input id="tipo_cliente-{{$loop->iteration}}" type="text" name="irpf" value="{{ old('irpf') }}" class="form-control" placeholder="Introduce el porcentaje de IRPF (sin el %)" maxlength="2" pattern="[0-9]+" required/>
        @endif
    @endforeach

        <div class="text-right">
        <input type="hidden" name="id_client" value="">
        <button type="submit" class="btn btn-success waves-effect waves-light">Create invoice</button>
    </div>

</form>

注意:当您在循环中执行此操作时,您需要注意id 的唯一性。我已经通过在输入 ID 末尾添加 $loop-&gt;iteration 来处理它。

【讨论】:

  • 但这不是一个解决方案,因为每次用户拥有该非空字段时都会出现一个文本框。我想要做的是,当从列表中选择一个用户时,会出现带有其值的输入,或者它显示为空。但始终与所选用户核对。
  • 如果您想在页面上显示动态内容,您应该使用 javascript 或其变体来实现。
猜你喜欢
  • 2017-10-08
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2016-02-14
  • 2013-09-06
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
相关资源
最近更新 更多