laravel 失败时在laravel中填充旧的html选择字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26173572/
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
populate old html select field in laravel upon failure
提问by dxcoder1
i have a form with a select input, validation is fine, but upon failure the select field doesn't populate the old value here is my select field
我有一个带有选择输入的表单,验证很好,但是失败时选择字段不会填充旧值 这里是我的选择字段
<div class="control-group">
<label class="control-label">Gender :</label>
<div class="controls">
<select name="gender" value= "{{ Input::old('gender') }}">
<option>Select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</div>
</div>
how can i solve this?
我该如何解决这个问题?
回答by Marcin Nabia?ek
If you don't want to use Laravel Form build, you need to do it this way:
如果你不想使用 Laravel Form 构建,你需要这样做:
<div class="control-group">
<label class="control-label">Gender :</label>
<div class="controls">
<select name="gender">
<option>Select</option>
<option value="M" @if (Input::old('gender') == 'M') selected="selected" @endif>Male</option>
<option value="F" @if (Input::old('gender') == 'F') selected="selected" @endif>Female</option>
</select>
</div>
</div>
回答by Henrik
I personally think this code is cleaner. There is no right or wrong here, I can understand why some would prefer the Laravel @if @endif statements, I just think they look too visually disturbing.
我个人认为这段代码更简洁。这里没有对错之分,我能理解为什么有些人更喜欢 Laravel @if @endif 语句,我只是觉得它们看起来太令人不安了。
<option {{ old('gender')=='male' ? 'selected="selected"' : '' }}>Test option</option>
回答by Varun Varunesh
This is due to HTML select element.
这是由于 HTML 选择元素。
If you want to assign some default option value to the select element use the attribute selected="selected" for that particular option otherwise by default it will show the first option value.
如果您想为 select 元素分配一些默认选项值,请为该特定选项使用属性 selected="selected" 否则默认情况下它将显示第一个选项值。
<select name="gender">
<option>Select</option>
<option value="M" @if (Input::old('gender') == 'M') selected="selected" @endif>Male</option>
<option value="F" @if (Input::old('gender') == 'F') selected="selected" @endif>Female</option>
</select>
回答by Leonardo Hessel
This is the way I do, very dynamic.
这就是我所做的,非常有活力。
<select id="gender" name="gender">
<option>Select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
<script>
var currentGender = null;
for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
{
currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
if(currentGender.getAttribute("value") == "{{ old("gender") }}")
{
currentGender.setAttribute("selected","selected");
}
}
</script>
回答by bdereta
I would recommend using Laravel Collectivepackage.
我建议使用Laravel Collective包。
{!! Form::label('gender', 'Gender', ['class' => 'control-label']) !!}
{!! Form::select('gender',
['M' => 'Male', 'F' => 'Female'],
'F', //set default value (or null)
['class' => 'form-control', 'required' => true,]
) !!}
It takes care of the old value without using that ugly if-else injection into every select value.
它处理旧值,而不使用丑陋的 if-else 注入到每个选择值中。