【发布时间】:2017-12-22 11:46:41
【问题描述】:
我有道具:['attributes']
{
"name": "Color",
"variant": [
{
"name": "Red"
},
{
"name": "Green"
},
{
"name": "Blue"
}
]
},
{
"name": "Size",
"variant": [
{
"name": "L"
},
{
"name": "XL"
},
{
"name": "XXL"
}
]
}
在模板中:
<div class="form-group" v-for="(attribute, index) in attributes">
{{attribute.name}}
<div v-for="(variant, vindex) in attribute.variant">
<input type="radio"
:value="[{name: attribute.name, variant:variant.name}]"
:id="'radio' + vindex"
:name="'group' + index">
{{variant.name}}
</div>
</div>
结果: enter image description here
问题:如何在数组中返回选定的单选按钮?例如:
[{
"name": "Color",
"variant":
{
"name": "Blue"
}
},
{
"name": "Size",
"variant":
{
"name": "XL"
}
}]
单选按钮不会作为复选框添加到数组中。
【问题讨论】:
-
您的点击处理程序在哪里?
-
点击处理程序仅在数组中添加 ** selectVariants: function(vindex) { var input = document.getElementById('radio' + vindex); this.attr.push(input.value); }**
-
您需要@click 处理程序将单选值绑定到您的数据
-
你能帮我吗?一个简单的处理程序:如果 cheked - 在数组中添加,否则在数组中删除。问题是你需要添加和删除一个对象。
-
您可以自己轻松完成。只需检查 vue 介绍中的点击事件
标签: json vue.js radio-button