You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue中v-for循环内使用v-if时如何为activate按钮添加禁用条件

解决方案

你可以直接在v-else对应的activate按钮上,通过Vue的动态属性绑定disabled实现需求。disabled是HTML原生的按钮禁用属性,用:(v-bind的简写)绑定后就可以响应data里的变量变化,值为true时按钮会被禁用,值为false时恢复可用。

修改后的代码如下:

<button v-if="Collection.status" type="button">deactivate</button>
<button v-else type="button" :disabled="disableButton">activate</button>

额外说明:

  • 如果disableButton是组件全局的data变量,上面的写法可以直接生效,所有渲染出来的activate按钮都会跟随这个变量的状态控制是否禁用
  • 如果你需要每个遍历项的禁用状态独立控制,可以把禁用字段存在遍历的数组对应项中,写法调整为:disabled="Collection.disableButton"即可

内容的提问来源于stack exchange,提问作者iSnuff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 10:45:03