我有多个组,每个组中有多个选择。当我点击组内的一个选项时,groupcount
应该总是+ 1,点击的choicecount
应该设置为1,而组内的其他choicecount
应该重置为0。
在设置choicecount = 1
和groupcount++
之前,我尝试使用$dispatch
将组内的所有choicecount
重置为0,但$dispatch
似乎不会触发组内其他嵌套的choicecount
。
例如,在下面的代码片段中(第1组):G1 Option 1
,G1 Option 2
,G1 Option 3
每次只允许一个等于1。如果G1 Option 2
= 1,那么G1 Option 1
和G1 Option 3
应该= 0。groupcount
工作正常
注意:这是剥离的,单选按钮不适合我正在创建的功能,我不使用x-for
,因为这些组和选择将从PHP循环生成。分组和选择的数量会有所不同。
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<!-- GROUP 1 -->
<div x-data="{ groupcount: 0 }">
<label>Group 1 Count</label>
<input x-model="groupcount" type="text">
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G1 Option 1
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G1 Option 2
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G1 Option 3
</button>
</div>
</div>
</div>
<hr>
<!-- GROUP 2 -->
<div x-data="{ groupcount: 0 }">
<label>Group 2 Count</label>
<input x-model="groupcount" type="text">
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G2 Option 1
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G2 Option 2
</button>
</div>
</div>
</div>
<hr>
<!-- GROUP 3 -->
<div x-data="{ groupcount: 0 }">
<label>Group 3 Count</label>
<input x-model="groupcount" type="text">
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G3 Option 1
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:resetAllCountsInGroup="choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('resetAllCountsInGroup'),choicecount=1,groupcount++">
G3 Option 2
</button>
</div>
</div>
</div>
.window
修饰符。其次,HTML属性中不支持camel大小写,因此我们需要在事件名称中使用破折号(或使用.camel
修饰符),我将在示例中使用reset-group
。最后一个问题是,重置事件现在作用于每个组,因此我们需要将其范围限制为当前组。为此,我为每个组添加了一个groupid
属性,这是唯一的,它也包含在事件中,因此我们可以在重置组之前使用if (groupid == $event.detail) choicecount=0
在事件侦听器中检查其值。
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<div x-data="{ groupcount: 0, groupid: 1 }">
<label>Group 1 Count</label>
<input x-model="groupcount" type="text">
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G1 Option 1
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G1 Option 2
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G1 Option 3
</button>
</div>
</div>
</div>
<div x-data="{ groupcount: 0, groupid: 2 }">
<label>Group 2 Count</label>
<input x-model="groupcount" type="text">
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G2 Option 1
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G2 Option 2
</button>
</div>
</div>
<div x-data="{ choicecount: 0 }">
<div x-on:reset-group.window="if (groupid == $event.detail) choicecount=0">
<input x-model="choicecount" type="text">
<button type="button" x-on:click="$dispatch('reset-group', groupid),choicecount=1,groupcount++">
G2 Option 3
</button>
</div>
</div>
</div>