Laravel 表单提交只适用于 livewire 中的提交按钮

Laravel Form Submit should only work with submit button in livewire

首先,我创建了一个表单并将其连接到函数但是它有两个按钮,一个按钮用于表单提交,另一个按钮用于重置所有内容。但是,如果按下任何按钮,它将在 运行 resetcategory() 函数之后提交,但在它之后 运行 submitcategory() 我无法得到我使用 if($this->name) 的一些解决方法,但我不'想用那个。请帮助?

我的 php livewire 文件是

public function resetcategory()
{
    $this->reset();
}

public function submitcategory()
{
     if ($this->name)
    {
    Category::create([
        'name' => $this->name,
        'parent' => $this->parent,
    ]);
    $this->reset();
    }
}

我的blade文件是---

 <form wire:submit.prevent="submitcategory">
      <div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
        <!-- here is input content-->
        <button class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition mr-2 ml-2" wire:click="resetcategory()">Reset</button>
    
        <button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition">Send</button>
      </div>
    </form>

这是因为您没有在 Reset 按钮上指定 type。按钮的默认 type(如果未提供 type)是 submit。所以你的重置按钮实际上也在提交表单。

Reset 按钮上添加 type="button" 以防止它提交表单。

<button type="button" wire:click="resetcategory()">Reset</button>