微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Livewire 表单内容消失

如何解决Livewire 表单内容消失

我在包含表单的 livewire 视图中有此代码。这是一个简单的选择字段,您应该在其中从列表中选择文档类型。

<div>
    <label for="documenttype" class="form-label"><strong>Document type</strong></label>
    <select wire:model="documenttype_id" class="form-control" id="documenttype" aria-describedby="documenttypeHelp">
        <option selected>{{ __( 'Choose' ) }}</option>
        @foreach( $documenttypes as $documenttype )
            <option value="{{ $documenttype->id }}" @if( isset( $documenttype_id ) && ( $documenttype_id == $documenttype->id ) ) selected @endif>
                {{ $documenttype->name }}
            </option>
        @endforeach
    </select>
</div>

问题是我希望它在编辑客户端时预先选择了正确的值。但是出于某种原因,当我在选项循环中添加代码时:@if( isset( $documenttype_id ) && ( $documenttype_id == $documenttype->id ) ) selected @endif,表单开始出现奇怪的行为。 每次填写表单的人选择一个选项时,内容都会变为空白。

这是情况 1(在选择选项之前):

enter image description here

这是情况2(选择选项后):

enter image description here

我不知道为什么会发生这种情况,有人可以指导我如何解决这个问题吗?

解决方法

解决此问题的快速方法是从您的组件 mount 方法中分配选定的值。

public function mount() {

    $this->documenttype_id = $client->documenttype_id
    //$client->documenttype_id being the previously selected value from your db
}

这将自动选择您的选项,您不需要选项标签上的 if 语句。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。