javascript - vuejs怎么实现v-for中的单选按钮
ringa_lee
ringa_lee 2017-04-11 10:14:40
[JavaScript讨论组]
  <p  v-for="subject in subjects" v-bind:class="classObject" >{{subject.question}}
            <input type="radio" id="a" value="a" name="forradio" v-on:click="check_question">
            <label for="a">{{subject.answer_a}}</label>
            <input type="radio" id="b" value="b"  name="forradio"  v-on:click="check_question">
            <label for="b">{{subject.answer_b}}</label>
            <input type="radio" id="c" value="c" name="forradio"  v-on:click="check_question">
            <label for="c">{{subject.answer_c}}</label>
            <input type="radio" id="d" value="d" name="forradio"  v-on:click="check_question">
            <label for="d">{{subject.answer_d}}</label>
            </p>

这样不行,会导致所有单选只能选择一个

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
PHPz

解决了。

  <p id="subject-list-item">
        <ol>
           
            <p v-for="subject in subjects">
                <li>
                    {{subject.question}}
                    <input type="radio" v-model="subject.answer_e" v-bind:value="subject.answer_a" >
                    <span>{{subject.answer_a}}</span>
                    <input type="radio" v-model="subject.answer_e" v-bind:value="subject.answer_b" >
                    <span>{{subject.answer_b}}</span>
                    <input type="radio" v-model="subject.answer_e" v-bind:value="subject.answer_c" >
                    <span>{{subject.answer_c}}</span>
                    <input type="radio" v-model="subject.answer_e" v-bind:value="subject.answer_d" >
                    <span>{{subject.answer_d}}</span>


                </li>
            </p>
        </ol>
    </p>

这里需要注意,v-model会变成value的值,所以不能单纯的从官网的例子上照抄。而是需要将v-model设置为一个动态的值。

大家讲道理

在 v-for 块中,我们拥有对父作用域属性的完全访问权限。 v-for 还支持一个可选的第二个参数为当前项的索引。

<ul id="example-2">
  <li v-for="(item, index) in items">
    {{ parentMessage }} - {{ index }} - {{ item.message }}
  </li>
</ul>

https://cn.vuejs.org/v2/guide...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号