[英]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.