选中 radio 或者 checkbox 需要注意的是:
不管 < input type='radio checked='true''> 你的 checked 属性值是 true 或者 false, 他都会选中.
选中不选中, 不是看 checked 的属性值, 而是看有没有 checked 这个属性, 所以, 动态选中, 不用 v-model, 也不用 checked='true', 只需要判断渲染 checked 这个属性就好了.
不多说了, 直接上代码
- <div v-for="(item,index) in product_test">
- <input name="Fruit" class="input-fruit" :checked="product_test_index_selected==index?true:false" type="radio" @change="onRadioChange(index)">
- </div>
JS 部分
- data() {
- return {
- product_test_index_selected: 0, // radio 默认选择第一个
- };
- },
- onRadioChange(index) {
- this.product_test_show_index_selected = index;
- },
想实现每次点击之后选项默认选择为第一个, 需要在使用完 product_test_index_selected 之后, 将其更新为 0, 表示 radio 选中第一个
来源: http://www.bubuko.com/infodetail-2944684.html