Vue3表格数据绑定到Input框:使用v-model实现双向数据绑定
在Vue3中,可以使用'v-model'指令将表格中的数据绑定到输入框中。
首先,在表格中使用'v-for'指令遍历数据,并将每个数据项绑定到相应的输入框中:
<table>
<tr v-for='(item, index) in tableData' :key='index'>
<td>
<input type='text' v-model='item.value'>
</td>
</tr>
</table>
在上面的例子中,'tableData'是一个数组,每个数组项都包含一个'value'属性,该属性对应输入框中的值。
接下来,在Vue组件中,定义'tableData'数组,并初始化数据:
export default {
data() {
return {
tableData: [
{ value: 'Value 1' },
{ value: 'Value 2' },
{ value: 'Value 3' }
]
}
}
}
现在,表格中的数据将会自动显示在相应的输入框中,并且任何对输入框的更改都会自动更新'tableData'数组中的对应项的值。
原文地址: https://www.cveoy.top/t/topic/qvRC 著作权归作者所有。请勿转载和采集!