【问题标题】:VUE 2 with Bootstrap 4 - Radio button action not working when using data-toggle="buttons"带有 Bootstrap 4 的 VUE 2 - 使用 data-toggle="buttons" 时单选按钮操作不起作用
【发布时间】:2019-11-08 02:40:45
【问题描述】:

我有以下代码:

<template>
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-outline-dark active">
            <input type="radio" name="grp" v-model="channel" value="vh1" checked> VH1
        </label>
        <label class="btn btn-outline-dark">
            <input type="radio" name="grp" v-model="channel" value="mtv"> MTV
        </label>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                channel: 'mtv'
            }
        },
        watch: {
            channel: function(newValue) {
                console.log('value has been changed to ' + newValue);
            }
        }
     }
</script>

当我单击单选按钮时,什么也没有发生。但是,当我删除样式的“data-toggle="buttons" 属性时,它就开始工作了!谁能帮我在这里找到工作?

编辑

对于那些不知道的人,data-toggle 来自引导按钮插件,它为您正在制作的按钮添加了额外的样式和功能。在此处查看文档: https://getbootstrap.com/docs/4.0/components/buttons/#toggle-states

【问题讨论】:

    标签: vuejs2 bootstrap-4


    【解决方案1】:

    删除data-toggle 并为每个输入设置active:class="{ 'active': channel === [value] }"

    【讨论】:

    • 谢谢,这对我来说很完美。这应该是公认的答案。
    • 谢谢!这更简单,我喜欢它,更少的活动部件。因为我添加了一个深色主题,所以我这样做了: :class="{'active': channel === [value], 'btn' : 'btn', 'btn-outline-dark' : 'btn-outline -dark'}"
    • 我遇到了同样的问题并尝试实施解决方案。但是我不明白你从哪里得到[value]?那是引用单选按钮的值吗?括号中的值来自哪里?谢谢
    • @drpcken [value] 是你在输入标签的 value 属性中设置的。
    • 这就是我的假设,但是您需要单选按钮的[value] 才能工作,对吧? active 类必须设置为单选按钮的标签,而不是实际的单选类(对吗?)。我通过将我正在使用的收音机的值放入数据对象数组中并使用标签中的v-for 遍历它们并像上面一样设置active 类来解决它。
    【解决方案2】:

    终于解决了

    经过长时间的搜索,我想出了一种破解按钮的方法。

    第 1 步

    将上面的模板更改为以下(从输入中删除了 v-model)

    <template>
        <div class="btn-group" data-toggle="buttons" v-radio="channel">
            <label class="btn btn-outline-dark active">
                <input type="radio" name="grp" value="vh1" checked> VH1
            </label>
            <label class="btn btn-outline-dark">
                <input type="radio" name="grp" value="mtv"> MTV
            </label>
        </div>
    </template>
    

    这个解决方案很难找到,而且解决方案也很老套,但它完成了工作。

    第 2 步(2018 年 6 月 18 日更新)

    创建指令

    在我的例子中,由于我使用的是单个文件组件,我需要通过以下方式将指令带入:

    radio.js

    export default {
        inserted: function (el, binding) {
            var btns = $(el).find('.btn');
            var radioGrpName = $(btns[0]).find('input')[0].name;
            $("input[name='" + radioGrpName + "'][value='" + binding.value + "']").closest('.btn').button('toggle');
        },
        bind: function (el, binding, vnode) {
            var btns = $(el).find('.btn');
            btns.each(function () {
                $(this).on('click', function () {
                    var v = $(this).find('input').get(0).value;
                    (function set(obj, str, val) {
                        str = str.split('.');
                        while (str.length > 1) {
                            obj = obj[str.shift()];
                        }
                        return obj[str.shift()] = val;
                    })(vnode.context, binding.expression, v);
                })
            })
        }
    }
    

    它的作用是将 jquery click 事件绑定到包含 v-radio 的 div 中的每个单选按钮。我正在做函数集的第二部分(从参考答案中复制),它检查表达式中是否有任何点,否则它设置 vnode.context["channel"] 的值,它会更新模型.绑定在组件加载之前与事件挂钩,因此它可以准备触发单选按钮的内部工作。

    inserted 函数在绑定之后调用,在组件物理插入父节点期间。所以当你设置一个初始状态(没有触发事件)时,组件会自动反映数据中设置的值。

    第 3 步

    将指令无线电添加到脚本

    import radio from '../../directives/radio'
    <script>
        export default {
            directives: {
                radio: radio  
            },
            data() {
                return {
                    channel: 'mtv'
                }
            },
            //you can use computed property, but you need get and set
            watch: {
                 channel: function(newValue) {
                     console.log('value has been changed to ' + newValue);
                 }
             }
         }
     </script>
    

    使用这些链接作为参考:

    【讨论】:

    • 不错的解决方案。但我认为“inserted: function (el) {”行中缺少辅助参数“binding”
    • 嗨BitLord,感谢您的建议。自从我这样做以来已经有一段时间了,但我会相信你的话。更新了答案。
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2018-10-08
    相关资源
    最近更新 更多