【问题标题】:Get checkbox status in livewire在 livewire 中获取复选框状态
【发布时间】:2021-06-08 18:31:37
【问题描述】:

我有一个表单,该表单中有 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');
    }
}

【问题讨论】:

  • 使用wire:model绑定属性从元素到组件

标签: laravel laravel-livewire


【解决方案1】:

您可以使用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>

现在提交表单时,如果depositwithdrawchecked,它们的值将是true

您可能想要做的事情是给$deposit$withdraw 一个默认值false,否则它们的初始值将是null

public $deposit = false;

public $withdraw = false;

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2015-07-15
    • 2013-10-05
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多