laravel livewire 如果选择的选项是其他则添加动态输入

问题描述 投票:0回答:1
order.blade.php (step component)
<div class="mt-2">
        <x-input-label for="category" :value="__('Category')" />
        <x-input-select wire:model.change="state.category" :options="$categories" name="category" id="category" class="block mt-1 w-full" />
        <x-input-error :messages="$errors->get('category')" class="mt-2" />

        @if($selectedState === '6')
            <x-input-label for="others" :value="__('Specify category')" />
            <x-text-input wire:model="state.others" id="others" class="block mt-1 w-full" name="others" :value="old('others')" />
            <x-input-error :messages="$errors->get('others')" class="mt-2" />
        @endif

    </div>

Order.php
    public function updatedStateCategory($name, $value)
    {
        // Something you want
        $this->selectedState = $name;
    }

如果选择其他选项,则显示文本输入,但刷新或提交表单时,输入消失

select state laravel-livewire alpine.js
1个回答
0
投票

selectedState必须在渲染视图之前预设:

public function render()
{
    $this->selectedState = $this->state['category'];
    return view(/* your view */);
}

© www.soinside.com 2019 - 2024. All rights reserved.