简体   繁体   English

复选框切换器 VUE JS

[英]Checkbox switcher VUE JS

I am trying to toggle this checkbox switcher.我正在尝试切换此复选框切换器。 It is checked by default and when unchecked, a button should appear.默认情况下已选中,未选中时,应显示一个按钮。 During the checked, there should be a function to call.在检查期间,应该有一个 function 可以调用。 What am i missing?我错过了什么?

<div class="form-group row m-b-10">
            <label class="col-form-label">Auto-Refresh:</label>
            <div class="col-md-9">
              <div class="switcher switcher-success">
                                    <input type="checkbox" v-model="checked" name="switcher_checkbox_2" id="switcher_checkbox_2" checked="" value="1">
                                    <label for="switcher_checkbox_2"></label>
                                </div>

              </div>
              <div class="col-md-9" v-if="unchecked">
<button @click="logNode(namespace)" class="btn btn-xs btn-success" >Refresh</button>
</div>
              </div>

Take a look at this codesandbox snippet看看这个代码框片段

Create methods for checkbox and button like below and define their own behaviour:为复选框和按钮创建方法,如下所示并定义它们自己的行为:

data: function() {
  return {
    checked: true
  };
},
methods: {
    logNode: function(myNamespace) {
      alert("button pressed");
    },
   toggle: function() {
      alert("checkbox triggered");
  }
}

Your html will look like this:您的 html 将如下所示:

<template>
  <div class="form-group row m-b-10">
    <label class="col-form-label">Auto-Refresh:</label>
    <div class="col-md-9">
      <div class="switcher switcher-success">
      <input
        type="checkbox"
        v-model="checked"
        name="switcher_checkbox_2"
        id="switcher_checkbox_2"
        checked="true"
        value="1"
        v-on:change="toggle">
      <label for="switcher_checkbox_2"></label>
    </div>
  </div>
  <div class="col-md-9" v-if="!checked">
    <button @click="logNode(namespace)" class="btn btn-xs btn-success">Refresh</button>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM