Laravel Blade - 如何让复选框在提交后保持选中状态
Laravel Blade - How to make checkbox stay checked after submit
我想创建一个功能来过滤数据。我使用复选框来做到这一点。
但是我想当复选框被选中然后用户提交过滤数据时,之前选中的复选框保持选中状态。
我试过这样做但是没用
<input type="checkbox" id="ac" name="ac" value="ac" @if(old('ac')) checked @endif>
我的表单提交方法是 GET。
old()
辅助函数仅用于保存 Flash 会话数据。如果不将其保存在 Flash 会话中,您将无法检索它。 old()
函数
保存闪存数据的例子
request()->flashOnly(['ac']);
或
return redirect('form')->withInput();
如果您使用 GET 方法并且在提交后没有重定向,您可以使用 request()
辅助函数来完成。像这样:
<input type="checkbox" id="ac" name="ac" value="ac" @if(request()->ac) checked @endif>
对于使用POST方法提交表单的多个复选框,
@foreach ($categories as $index => $category)
<div class="form-check form-check-inline">
<input type="checkbox" name="type[]"
value="{{ $category->id }}"
{{ (is_array(old('type')) && in_array($index + 1, old('type'))) ? 'checked' : '' }}
class="form-check-input @error('type') is-invalid @enderror"
id="type_{{$category->id}}">
<label class="form-check-label" for="type_{{$category->id}}">
{{ $category->name }}
</label>
</div>
@endforeach
这对我有用:
<input name="mail" type="hidden" value="0" >
<input name="mail" type="checkbox" value="1"@if(old('mail')) checked @endif>
控制器:
$request->validate([
'mail' => 'required',
]);
Contact::create($request->all());
我想创建一个功能来过滤数据。我使用复选框来做到这一点。 但是我想当复选框被选中然后用户提交过滤数据时,之前选中的复选框保持选中状态。
我试过这样做但是没用
<input type="checkbox" id="ac" name="ac" value="ac" @if(old('ac')) checked @endif>
我的表单提交方法是 GET。
old()
辅助函数仅用于保存 Flash 会话数据。如果不将其保存在 Flash 会话中,您将无法检索它。 old()
函数
request()->flashOnly(['ac']);
或
return redirect('form')->withInput();
如果您使用 GET 方法并且在提交后没有重定向,您可以使用 request()
辅助函数来完成。像这样:
<input type="checkbox" id="ac" name="ac" value="ac" @if(request()->ac) checked @endif>
对于使用POST方法提交表单的多个复选框,
@foreach ($categories as $index => $category)
<div class="form-check form-check-inline">
<input type="checkbox" name="type[]"
value="{{ $category->id }}"
{{ (is_array(old('type')) && in_array($index + 1, old('type'))) ? 'checked' : '' }}
class="form-check-input @error('type') is-invalid @enderror"
id="type_{{$category->id}}">
<label class="form-check-label" for="type_{{$category->id}}">
{{ $category->name }}
</label>
</div>
@endforeach
这对我有用:
<input name="mail" type="hidden" value="0" >
<input name="mail" type="checkbox" value="1"@if(old('mail')) checked @endif>
控制器:
$request->validate([
'mail' => 'required',
]);
Contact::create($request->all());