在 livewire 中获取复选框状态
Get checkbox status in livewire
我有一个表单,该表单中有 2 个复选框。当我提交信息时,我想测试第一个复选框的值是多少。我使用 `dd () 但它向我显示了 Null 的值。问题是什么?我该如何解决?
这是视图:
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="deposit" type="checkbox"> <label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="withdraw" type="checkbox"> <label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
<button class="btn btn-round btn-outline-primary"><span class="w-100px">create</span></button>
这是组件:
<?php
namespace App\Http\Livewire\Backend\Currency;
use Livewire\Component;
class Networks extends Component
{
public $deposit;
public $withdraw;
public function addNetwork()
{
dd($this->deposit1);
}
public function render()
{
return view('livewire.backend.currency.networks');
}
}
您可以像绑定任何其他 public 属性 一样绑定到复选框,使用 wire:model
。
<div class="form-group">
<div class="custom-control custom-switch">
<!-- bind to $deposit -->
<input wire:model="deposit"
class="custom-control-input"
id="deposit"
type="checkbox">
<label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<!--bind to $withdraw -->
<input wire:model="withdraw"
class="custom-control-input"
id="withdraw"
type="checkbox">
<label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
现在提交表单时,如果deposit
或withdraw
是checked
,它们的值将是true
。
您可能想要做的是给 $deposit
和 $withdraw
一个默认值 false
,否则它们的初始值将是 null
.
public $deposit = false;
public $withdraw = false;
我有一个表单,该表单中有 2 个复选框。当我提交信息时,我想测试第一个复选框的值是多少。我使用 `dd () 但它向我显示了 Null 的值。问题是什么?我该如何解决?
这是视图:
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="deposit" type="checkbox"> <label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="withdraw" type="checkbox"> <label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
<button class="btn btn-round btn-outline-primary"><span class="w-100px">create</span></button>
这是组件:
<?php
namespace App\Http\Livewire\Backend\Currency;
use Livewire\Component;
class Networks extends Component
{
public $deposit;
public $withdraw;
public function addNetwork()
{
dd($this->deposit1);
}
public function render()
{
return view('livewire.backend.currency.networks');
}
}
您可以像绑定任何其他 public 属性 一样绑定到复选框,使用 wire:model
。
<div class="form-group">
<div class="custom-control custom-switch">
<!-- bind to $deposit -->
<input wire:model="deposit"
class="custom-control-input"
id="deposit"
type="checkbox">
<label class="custom-control-label" for="deposit">Deposit</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="custom-control custom-switch">
<!--bind to $withdraw -->
<input wire:model="withdraw"
class="custom-control-input"
id="withdraw"
type="checkbox">
<label class="custom-control-label" for="withdraw">withdraw</label>
</div>
</div>
</div>
现在提交表单时,如果deposit
或withdraw
是checked
,它们的值将是true
。
您可能想要做的是给 $deposit
和 $withdraw
一个默认值 false
,否则它们的初始值将是 null
.
public $deposit = false;
public $withdraw = false;