簡體   English   中英

如何在 Laravel Livewire 中傳遞隱藏的表單輸入?

[英]How can I pass a hidden form input in Laravel Livewire?

最近我一直在嘗試實現 Laravel 的 livewire 來提交表單和刷新頁面,而實際上沒有使用基於 ajax 的當前流程。 除了我需要發送一些隱藏的輸入值之外,它適用於所有其他內容,我將在提交表單之前根據用戶的操作進行更改。

<input type="text" class="w-100 ratings-hidden" value="" wire:model="rating_val">
<input class="" value=""  wire:model="reviewable_id"  type="hidden">
<textarea class="form-control w-100 animated" cols="50" id="new-review" wire:model="comment"  placeholder="Enter your review here..." rows="5"></textarea>

在這里,提交后可以很好地獲取評論,但我無法獲得 rating_val 和 reviewable_id 的值

從輸入中刪除 value="" 因為 livewire 會為您完成。

在您的組件中設置屬性,您需要 ($rating_val, $reviewable_id) 並且在您的掛載方法中,您初始化這些屬性的值。

class ComponentName extends Component
{
   public $rating_val;
   public $reviewable_id;



  public function mount() 
  {
     $this->rating_val = "value_for_input";
     $this->reviewable_id = "value_for_input";
  }
}

在您的刀片文件中,您只需要以下內容

<input type="hidden" wire:model="rating_val">
<input type="hidden" wire:model="reviewable_id">

https://laravel-livewire.com/docs/2.x/properties

您不能使用隱藏值或隱藏輸入,但您可以使用此解決方案:

在你的刀片中:

<form wire:submit.prevent="YourMethodName( {{ $param1 }} , {{ $param2 }})")>
    <button type="submit" >Sned</button>
</form>

在您的組件中:

public function YourMethodName($param1 , $param2)
    {
        dd([
            'param1'=> $param1 ,
            'param2' => $param2,
        ]);
    }

它應該可以工作,在我使用此解決方案的項目中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM