我用这个循环
<form action="/" method="POST">
<template x-for="(deck, index) in $store.cart.decks">
<div x-show="$store.cart.total(index) > 0">
<div><span x-text="$store.cart.total(index)"></span> Karten</div>
<div x-text="deck.price"></div> €
<input :name="'deck[' + index + '][name]'" :value="$store.cart.decks[index].name">
</div>
</template>
</form>
这工作正常并显示我的购物车中的所有项目并隐藏那些总共为 0 的项目。但这只会将
display:none
上的项目设置为 0 ,这将 仍然传输POST请求中的数据 .x-if
而不是 x-show
但这仅在 tempate 标签上允许。<template x-show="$store.cart.total(index) > 0" ...
这会导致错误,因为 index
在那一点上没有设置<template>
标记在现有模板中,然后是变量 index
和 deck
在第二个内不再可用如何防止在我的 POST 请求中计算零购物车项目?
最佳答案
我认为类似以下内容会起作用,请注意 div
两者内template
元素。
<template x-for="(deck, index) in $store.cart.decks">
<div>
<template x-if="$store.cart.total(index)">
<div>
<!-- rest of the content -->
</div>
</template>
</div>
</template>
关于forms - 使用 x-if 跳过 alpine.js 中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65547248/