js-vue中多个按钮状态选中类似于复选框与单选框实现

1.vue中多个按钮状态选中类似于复选框

在Vue中处理多个按钮的选中状态切换,通常我们会利用Vue的响应式数据系统来追踪每个按钮的选中状态。

html

html 复制代码
<div id="app">  
  <button  
    v-for="button in buttons"  
    :key="button.id"  
    :class="{ active: button.isSelected }"  
    @click="toggleSelection(button.id)"  
  >  
    {{ button.text }}  
  </button>  
</div>

js

javascript 复制代码
new Vue({  
  el: '#app',  
  data: {  
    buttons: [  
      { id: 1, text: '按钮1', isSelected: false },  
      { id: 2, text: '按钮2', isSelected: false },  
      { id: 3, text: '按钮3', isSelected: false }  
    ]  
  },  
  methods: {  
    toggleSelection(id) {  
      // 找到并点击的按钮并切换其选中状态  
      this.buttons.forEach(button => {  
        if (button.id === id) {  
          button.isSelected = !button.isSelected;  
        }  
      });  
    }  
  }  
});

css

css 复制代码
new Vue({  
  el: '#app',  
  data: {  
    buttons: [  
      { id: 1, text: '按钮1', isSelected: false },  
      { id: 2, text: '按钮2', isSelected: false },  
      { id: 3, text: '按钮3', isSelected: false }  
    ]  
  },  
  methods: {  
    toggleSelection(id) {  
      // 找到并点击的按钮并切换其选中状态  
      this.buttons.forEach(button => {  
        if (button.id === id) {  
          button.isSelected = !button.isSelected;  
        }  
      });  
    }  
  }  
});

定义一个buttons数组 ,其中包含了每个按钮的idtext(按钮上显示的文本)和isSelected按钮的选中状态)。

使用v-for指令来遍历buttons数组,并为每个按钮绑定了一个点击事件处理器toggleSelection,该处理器接收按钮的id作为参数。当按钮被点击时,toggleSelection方法会根据id找到对应的按钮,并切换其isSelected属性的值

使用:class绑定来根据按钮的isSelected状态动态添加active类,以改变按钮的样式来表示其选中状态。

2.vue中多个按钮状态选中类似于单选框

实现类似单选框的功能,即在一组按钮中只能同时选中一个,你可以通过维护一个变量来记录当前选中的按钮的id,并在点击按钮时更新这个变量。然后,根据这个变量来设置每个按钮的选中状态。

html 复制代码
<div id="app">  
  <button  
    v-for="button in buttons"  
    :key="button.id"  
    :class="{ active: selectedButtonId === button.id }"  
    @click="selectButton(button.id)"  
  >  
    {{ button.text }}  
  </button>  
</div>  
  
<script>  
new Vue({  
  el: '#app',  
  data: {  
    buttons: [  
      { id: 1, text: '按钮1' },  
      { id: 2, text: '按钮2' },  
      { id: 3, text: '按钮3' }  
    ],  
    selectedButtonId: null // 用来记录当前选中的按钮的id  
  },  
  methods: {  
    selectButton(id) {  
      // 更新当前选中的按钮id  
      this.selectedButtonId = id;  
    }  
  }  
});  
</script>  
  
<style>  
.active {  
  background-color: blue;  
  color: white;  
}  
</style>

buttons数组包含了所有按钮的信息,而selectedButtonId变量用于跟踪当前选中的按钮的id。每个按钮都绑定了一个点击事件处理器selectButton,当按钮被点击时,该处理器会更新selectedButtonId的值为被点击按钮的id

相关推荐
zhanghongbin013 小时前
AI 采集器:Claude Code、OpenAI、LiteLLM 监控
java·前端·人工智能
IT_陈寒3 小时前
Python的列表推导式里藏了个坑,差点让我加班到凌晨
前端·人工智能·后端
吴声子夜歌3 小时前
ES6——正则的扩展详解
前端·mysql·es6
天若有情6733 小时前
【C++原创开源】formort.h:一行头文件,实现比JS模板字符串更爽的链式拼接+响应式变量
开发语言·javascript·c++·git·github·开源项目·模版字符串
天***88524 小时前
Edge 浏览器离线绿色增强版+官方安装包,支持win7等系统
前端·edge
漫游的渔夫4 小时前
别再直接 `json.loads` 了!AI 返回的 JSON 坑位指南
前端·人工智能
软件工程师文艺4 小时前
从0到1:Claude Code如何用React构建CLI应用
前端·react.js·前端框架
M ? A4 小时前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
yuki_uix4 小时前
重排、重绘与合成——浏览器渲染性能的底层逻辑
前端·javascript·面试
Burt4 小时前
我的 2026 全栈选型:Vue3 + Elysia + Bun + AlovaJS
vue.js·全栈·bun