vuejs set a radio button checked if statement is true

前端 未结 4 1146
情话喂你
情话喂你 2020-12-08 13:22

I am trying to make a radio button checked using vuejs v-for only if my if-statement is true. Is there a way to use vuejs\' v-if/v-else for this type of problem?

in

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 14:11

    I would like to point out a few options when dealing with radios and vue.js. In general if you need to dynamically bind an attribute value you can use the shorthand binding syntax to bind to and calculate that value. You can bind to data, a computed value or a method and a combination of all three.

    new Vue({
      el: '#demo',
      data() {
        return {
          checkedData: false,
          checkedGroupVModel: "radioVModel3", //some defaul
          toggleChecked: false,
          recalculateComputed: null
        };
      },
      computed: {
        amIChecked() {
          let isEven = false;
          if (this.recalculateComputed) {
            let timeMills = new Date().getMilliseconds();
            isEven = timeMills % 2 === 0;
          }
          return isEven;
        }
      },
      methods: {
        onToggle() {
          this.toggleChecked = !this.toggleChecked;
          return this.toggleChecked;
        },
        mutateComputedDependentData() {
          this.recalculateComputed = {};
        }
      }
    })
    
    
    Simple Radio Group - Only one checked at a time. Bound to data.checkedData


    Understanding checked attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-checked

    Simple Radio - Checked bouned to semi-random computed object
     

    Simple Radio Group - v-model bound value = {{checkedGroupVModel}}



    Simpe Radio - click handler to toggle data bound to :checked to toggle selection

提交回复
热议问题