Laravel 5.1 如何在 blade 文件上使用 {{ old('') }} 助手进行无线电输入
Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs
我目前正在学习 laravel 并创建我的第一个表单。一切都很棒,直到我想在我的 blade 文件中为单选按钮使用 {{ old('') }} 助手。我不确定如何正确地做这件事,而且我似乎也无法在这里找到太多关于它的信息。
我的代码如下:
<div class="form-group">
<label for="geckoHatchling">Gecko Hatchling?</label>
<div class="radio">
<label>
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1">
Yes
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" checked>
No
</label>
</div>
</div>
您可以使用 Form-helper
,但它不会与 Laravel 一起使用。您应该手动安装它。 Please read the docs.
与 FORM-HELPER
1. Blade
{!! Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio')) !!}
{!! Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio')) !!}
2。 PHP
echo Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio'));
echo Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio'));
没有FORM-HELPER
1. Blade
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" @if(Input::old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" @if(!Input::old('geckoHatchling')) checked @endif>
2。 PHP
<input type="radio" name="geckoHatchling" value="1" class="radio" id="geckoHatchlingYes" <?php if(Input::old('geckoHatchling')== "1") { echo 'checked'; } ?> >
<input type="radio" name="geckoHatchling" value="0" class="radio" id="geckoHatchlingNo" <?php if(Input::old('geckoHatchling')== "0") { echo 'checked'; } ?> >
我认为以下内容更简洁:
<input type="radio" name="geckoHatchling" value="1" @if(old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" value="0" @if(!old('geckoHatchling')) checked @endif>
@if
正在检查旧值的真实性并在任何一种情况下输出 checked
。
我已经尝试了接受的答案的解决方案,它对我不起作用,
我不得不使用这个 {{}}
大括号(这是 blade 模板的回显)才能使用条件语句。
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" {{(old('geckoHatchling') == '1') ? 'checked' : ''}}>
我偶然发现了我认为会更准确、更全面地回答这个问题的答案。提供的主要答案的问题是最后一个单选按钮将始终被选中。但是,我在下面提供了一个代码示例来说明如何解决此问题。 (PS - 这适用于 Laravel 8,而且,我只是假设它适用于以前的版本)
<div> <input type="radio" name="role" value="teacher" @if(old('role') == 'teacher') checked @endif> </div>
<div> <input type="radio" name="role" value="student" @if(old('role') == 'student') checked @endif> </div>
<div> <input type="radio" name="role" value="assistant" @if(old('role') == 'assistant') checked @endif> </div>
正在使用:
@if(old('role') checked @endif
如果您有多个单选按钮,将始终选择最后一个单选按钮。
之前 Laravel 9
<input type"radio" name="active" value="1" {{old('active',user->active))?'checked',''}}/>
<input type"radio" name="active" value="0" {{old('active',user->active))?'','checked'}}/>
Laravel 9
<input type"radio" name="active" value="1" @checked(old('active',$user->active))/>
<input type"radio" name="active" value="0" @checked(!old('active',$user->active))/>
我目前正在学习 laravel 并创建我的第一个表单。一切都很棒,直到我想在我的 blade 文件中为单选按钮使用 {{ old('') }} 助手。我不确定如何正确地做这件事,而且我似乎也无法在这里找到太多关于它的信息。
我的代码如下:
<div class="form-group">
<label for="geckoHatchling">Gecko Hatchling?</label>
<div class="radio">
<label>
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1">
Yes
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" checked>
No
</label>
</div>
</div>
您可以使用 Form-helper
,但它不会与 Laravel 一起使用。您应该手动安装它。 Please read the docs.
与 FORM-HELPER
1. Blade
{!! Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio')) !!}
{!! Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio')) !!}
2。 PHP
echo Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio'));
echo Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio'));
没有FORM-HELPER
1. Blade
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" @if(Input::old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" @if(!Input::old('geckoHatchling')) checked @endif>
2。 PHP
<input type="radio" name="geckoHatchling" value="1" class="radio" id="geckoHatchlingYes" <?php if(Input::old('geckoHatchling')== "1") { echo 'checked'; } ?> >
<input type="radio" name="geckoHatchling" value="0" class="radio" id="geckoHatchlingNo" <?php if(Input::old('geckoHatchling')== "0") { echo 'checked'; } ?> >
我认为以下内容更简洁:
<input type="radio" name="geckoHatchling" value="1" @if(old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" value="0" @if(!old('geckoHatchling')) checked @endif>
@if
正在检查旧值的真实性并在任何一种情况下输出 checked
。
我已经尝试了接受的答案的解决方案,它对我不起作用,
我不得不使用这个 {{}}
大括号(这是 blade 模板的回显)才能使用条件语句。
<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" {{(old('geckoHatchling') == '1') ? 'checked' : ''}}>
我偶然发现了我认为会更准确、更全面地回答这个问题的答案。提供的主要答案的问题是最后一个单选按钮将始终被选中。但是,我在下面提供了一个代码示例来说明如何解决此问题。 (PS - 这适用于 Laravel 8,而且,我只是假设它适用于以前的版本)
<div> <input type="radio" name="role" value="teacher" @if(old('role') == 'teacher') checked @endif> </div>
<div> <input type="radio" name="role" value="student" @if(old('role') == 'student') checked @endif> </div>
<div> <input type="radio" name="role" value="assistant" @if(old('role') == 'assistant') checked @endif> </div>
正在使用:
@if(old('role') checked @endif
如果您有多个单选按钮,将始终选择最后一个单选按钮。
之前 Laravel 9
<input type"radio" name="active" value="1" {{old('active',user->active))?'checked',''}}/>
<input type"radio" name="active" value="0" {{old('active',user->active))?'','checked'}}/>
Laravel 9
<input type"radio" name="active" value="1" @checked(old('active',$user->active))/>
<input type"radio" name="active" value="0" @checked(!old('active',$user->active))/>