根据 php 中的复选框添加和减去值

Add and Substract values based on checkbox in php

我有一个简单的复选框表单,其中包含三个选项。我想动态计算价格,我不会将这些值存储在数据库中。

<div class="custom-control custom-checkbox">
   <input type="checkbox"id="option1" wire:click="test" wire:model="additions" value="option1">
   <label class="custom-control-label" for="option1">Option 1</label>
</div>
<div class="custom-control custom-checkbox">
   <input type="checkbox" id="option2" wire:click="test" wire:model="additions" value="option2">
   <label class="custom-control-label" for="option1">Option 2</label>
</div>
<div class="custom-control custom-checkbox">
   <input type="checkbox" id="option3" wire:click="test" wire:model="additions" value="option3">
   <label class="custom-control-label" for="option1">Option 3</label>
</div>

在 blade 文件中输出,我在其中显示值

{{ $checkout }}

每个选项都有不同的价格,我将添加到 $checkout 以计算总价。

public $additions = [];
public $checkout = 0;

public function test()
{
    // if (in_array('option1', $this->additions)) {
    //     $this->checkout =+ 10;
    //     Log::info($this->checkout);
    // }
    // if (in_array('option2', $this->additions)) {
    //    $this->checkout += 10;
    //     Log::info($this->checkout);
    // }
    // if (in_array('option3', $this->additions)) {
    //     $this->checkout += 10;
    //     Log::info($this->checkout);
    // }

    if (!empty($this->additions)) {
        foreach ($this->additions as $key => $value) {
            if ($value == 'option1') {
                $this->checkout += 10;
            }

            if ($value == 'option2') {
                $this->checkout += 10;
            } else {
                $this->checkout -= 10;
            }
            
            if ($value == 'option3') {
                $this->checkout += 10;
            } else {
                $this->checkout -= 10;
            }        
        }
        Log::info($this->checkout);
    }
}

价格增加但不正确。例如,我为每个选项设置了 10 个,所以当所有三个选项都被选中时,总数为 30,当两个选项都被选中时,总数为 20,依此类推。我得到的是 60 多岁,离 30 岁还差得很远。

如有任何帮助,我们将不胜感激。

已编辑: $this->additions 包含所有选中的项目。例如:如果选择所有三个选项,$this->additions 将在数组中包含 option1,option2,option3

首先,您的 Blade 视图应该只有一个根元素,所以您应该将整个元素包裹在一个 <div> 标签中。这是 Livewire 的事情,它相当重要,因为它使 Livewire 更容易进行 DOM-diffing。
其次,您不需要同时使用 wire:clickwire:model,因为您可以单独使用 wire:model 并通过 updated() 生命周期挂钩监听对绑定字段的更新 - 或者专门通过 updatedAdditions().

收听 additions 属性

然后,当您执行您的操作时,您应该为每次迭代重置计数器并避免减法 - 否则,您将增加每次点击的结帐计数器 - 这部分是您现在看到的。

此处更好的替代方法是使用包含每个选项值的查找数组,并对其进行聚合。

<div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option1" wire:model="additions" value="option1">
        <label class="custom-control-label" for="option1">Option 1</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option2" wire:model="additions" value="option2">
        <label class="custom-control-label" for="option1">Option 2</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="option3" wire:model="additions" value="option3">
        <label class="custom-control-label" for="option1">Option 3</label>
    </div>

    {{ $checkout }}
</div>
class Checkout extends Component
{
    public $additions = [];
    public $checkout = 0;

    public function updatedAdditions($value)
    {
        // Declare valid options and the value they increment
        $optionValues = [
            'option1' => 10,
            'option2' => 10,
            'option3' => 10,
        ];

        // Reset the counter before we iterate over the current additions
        $this->checkout = 0;

        // Iterate over additions, and find the value from $validOptions
        // If it doesn't exist in $validOptions, default to 0 (adding 0 makes no difference)
        foreach ($this->additions as $key => $value) {
            $this->checkout += $optionValues[$value] ?? 0;
        }
    }

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