vue.js - 如何为具有多个字段的对象数组创建 v-model
问题描述
所以我为数组中的每个元素有 2 个复选框输入。第一个表示表单中的一个字段,第二个表示该字段是否需要,我需要像这样构造对象数组
[
{
name: 'fieldName',
required: true
},
{
name: 'anotherFieldName'
}
]
如果第一个复选框被选中,我需要在我的数组中添加一个具有“name”属性的对象,如果第二个复选框也被选中,我还需要添加“required”字段。
解决方案
尝试这个
<li v-for="(item, index) of items">
{{ item.prop2 }}
<input type="text" v-model="items[index].prop2">
</li>
或者
<li v-for="(item, index) of items">
{{ item.prop2 }}
<input type="text" @input="updateMyProp(index)">
</li>
...
methods: {
updateMyProp ($event, index) {
// your update logic here
// you can use 'this.items', Object.assign, Vue.set, etc... to update your value
}
...
推荐阅读
- ios - 我们可以创建广播上传扩展的 Pod 或 swift 包吗?
- c# - 存储库主签名有效期已过。(NU3037)
- angularjs - 如何使用单元格模板访问 ng-model 数据?
- plot - 如何使用 matplotlib 绘制时间 (hh:mm)
- sql - 如何处理这些错误,PL/SQL 编译单元分析已终止 & PSL-00201:必须声明“表名”
- jquery - JQuery Remote(表单电子邮件验证)调用仅在您提交一次表单后才有效
- jsdoc - 将内容动态注入到 JSDoc @example
- flutter - Flutter HomeScreen(this.user) - 错误:位置参数太少:需要1个
- python - 带有 send_key 功能的 Tkinter 文件浏览器
- javascript - JSON解码不接收数据