简体   繁体   English

如何使用 laravel-livewire 对 2 个输入值求和

[英]How can i sum 2 input values using laravel-livewire

i want to sum 2 values.我想总结2个值。

my livewire blade codes:我的 livewire 刀片代码:

<label>Sayı 1:</label>
<input type="text" id="a" name="a" wire:model.lazy="a">
<br>

<label>Sayı 2:</label>
<input type="text" id="b" name="b" wire:model.lazy="b">
<br>
Sonuç : {{ $result}}

in livewire file在 livewire 文件中

public $result;
public $a;
public $b;

public function render()
{   
   $this->result=$this->a+$this->b;
    return view('livewire.try1');
}

i can not see the result on my page.我在我的页面上看不到结果。

i have changed $this->result=$this->a+$this->b;我改变$this->result=$this->a+$this->b; line to $this->result=a+b;行到$this->result=a+b; and it shows that undefined variable $a .它显示了undefined variable $a

i have opened debugbar a and b is null.我打开了调试栏 a 和 b 为空。

so i could not make it work.所以我无法让它工作。

First and foremost, your Livewire views should always be wrapped in a single div, kind of like this,首先,你的 Livewire 视图应该总是包裹在一个 div 中,有点像这样,

<div>
    <label>Sayı 1:</label>
    <input type="text" id="a" name="a" wire:model.lazy="a">

    <br>
    <label>Sayı 2:</label>
    <input type="text" id="b" name="b" wire:model.lazy="b">
    
    <br>
    Sonuç : {{ $result }}
</div>

Then I would suggest that you use a lifecycle-hook for updated to maintain the result,然后我建议您使用生命周期挂钩进行更新以维护结果,

public $result;
public $a;
public $b;

public function updated()
{
    $this->result = ($this->a ?? 0) + ($this->b ?? 0);
}

public function render()
{
    return view('livewire.try1');
}

Also, since you have the lazy modifier on your binding, beware that it will not update until you click away from the input, see https://laravel-livewire.com/docs/2.x/properties#lazy-updating此外,由于您的绑定上有lazy修饰符,请注意它不会更新,直到您点击离开输入,请参阅https://laravel-livewire.com/docs/2.x/properties#lazy-updating

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Laravel Livewire 中传递隐藏的表单输入? - How can I pass a hidden form input in Laravel Livewire? 如何从应用布局中的 laravel-livewire 组件访问声明的变量 - How to access declared variable from laravel-livewire component in the app layout Laravel-livewire:为什么触发事件会执行 render() 方法? - Laravel-livewire: Why does firing an event execute the render() method? 如何验证 Laravel 中的值的总和? - How can I validate the sum of the values in Laravel? 如何使用两个值的和来重定向表单并将其显示在Laravel中的只读输入字段中? - How can I redirect my form with the sum of two values and show it in a readonly input field in Laravel? 如果我们在输入上使用wire.model.defer,我们如何使用livewire在laravel中应用实时验证? - How can we apply real time validation in laravel using livewire, if we are using wire.model.defer on input? 我怎样才能在 laravel livewire 中检查多少个复选框 - how can i get how many checkboxes are checked in laravel livewire Laravel 8 &amp; laravel-livewire 简单的 CRUD,带有值数组而不是单个值 - Laravel 8 & laravel-livewire simple CRUD with array of value instead of single value 无法使用 Laravel Livewire 显示我的数据库中的值 - Can't display the values from my data base with Laravel Livewire 无法使用 Laravel Livewire 显示我的数据库中的值 - Can't display the values from my data base with Laravel Livewire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM