如何通过使用 alpine js 单击一个复选框来选中和取消选中所有复选框

Pra*_*pra 5 javascript alpine.js

我正在尝试使用 alpine js 通过分别使用 alpine js 选中或取消选中“全选”复选框来选中或取消选中所有框,任何人都可以指出我正确的方向...谢谢

And*_*ong 6

    <div x-data="selectBox()">
        <button x-on:click="selectAll">select all</button>
        <button x-on:click="unselectAll">unselect all</button>
        <template x-for="name in allNames">
            <input type="checkbox" :value="name" x-model="checkedNames">
        </template>
        <span x-text="JSON.stringify(checkedNames)"></span>
    </div>
Run Code Online (Sandbox Code Playgroud)
    <script>
        function selectBox() {
            return {
                checkedNames: [],
                allNames: ['bike', 'car', 'boat'],
                selectAll() { this.checkedNames = this.allNames },
                unselectAll() { this.checkedNames = []},
            }
        }
    </script>
Run Code Online (Sandbox Code Playgroud)