怎样讲vue中checkbox多选框选中的值传给后端
时间: 2024-05-10 16:20:47 浏览: 8
在 Vue 中,可以使用 v-model 指令来绑定数据,实现多选框的选中值传递给后端。具体步骤如下:
1. 在 HTML 中使用 v-model 绑定一个数组,用于存储多选框选中的值:
```html
<input type="checkbox" v-model="selectedFruits" value="apple"> Apple
<input type="checkbox" v-model="selectedFruits" value="banana"> Banana
<input type="checkbox" v-model="selectedFruits" value="orange"> Orange
```
2. 在 Vue 实例中定义 selectedFruits 数组,并将其初始化为空数组:
```javascript
new Vue({
el: '#app',
data: {
selectedFruits: []
}
})
```
3. 在提交表单时,将 selectedFruits 数组作为参数传递给后端:
```javascript
methods: {
onSubmit() {
axios.post('/api/submit', {
fruits: this.selectedFruits
}).then(response => {
console.log(response.data)
}).catch(error => {
console.log(error)
})
}
}
```
在后端接收到参数后,即可根据需要进行处理。