javascript - Uncheck radio button -


i'm trying set simple vue instance collection of radio buttons. goal that, if user clicks on radio button checked, uncheck respective radio button. couldn't using vue yet. here's code far:

html:

<div id="app">     <div v-for="(val, key) in list">         <input type="radio" name="radio" :value="val" v-model="selected" :id="val">         <label :for="val" @click="uncheck( val )">{{ val }}</label>     </div>     <button @click="uncheckall">uncheck all</button> </div> 

js:

var app = new vue({         el: '#app',         data : {             list: [ 'one', 'two', 'three' ],             selected: 'two',         },         methods : {             uncheck: function( val ){                 console.log( val, this.selected );                 if ( val == this.selected ){                     this.selected = false;                 }             },             uncheckall: function(){                 this.selected = false;             }         } }) 

seems uncheck method called, radio button triggers change event , updates value of selected again. uncheckall method works expected, because it's not tied data using v-model.

any tips or suggestions make work? here's pen created example: https://codepen.io/diegoliv/pen/jrpbbg

the difficulty v-model changing value of selected before can examine see previous value is. need variable.

the idea is: when click item, checks whether matches previouslyselected (rather selected). if matches, unselect. set previouslyselected value of selected.

the click handler should on input, not on label; function of label forward clicks input, anyway.

var app = new vue({    el: '#app',    data: {      list: ['one', 'two', 'three'],      selected: 'two',      previouslyselected: 'two'    },    methods: {      uncheck: function(val) {        if (val === this.previouslyselected) {          this.selected = false;        }        this.previouslyselected = this.selected;      },      uncheckall: function() {        this.selected = false;      }    }  })
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>  <div id="app">    <div v-for="(val, key) in list">      <input type="radio" name="radio" :value="val" v-model="selected" :id="val" @click="uncheck(val)">      <label :for="val">{{ val }}</label>    </div>    <button @click="uncheckall">uncheck all</button>  </div>

a more in-the-data way of approaching have computed based on selected, , have setter check re-selecting. no click handler. handled in normal course of setting value.

var app = new vue({    el: '#app',    data: {      list: ['one', 'two', 'three'],      d_selected: 'two'    },    computed: {      selected: {        get() {          return this.d_selected;        },        set(v) {          if (v === this.d_selected) {            this.d_selected = false;          } else {            this.d_selected = v;          }        }      }    },    methods: {      uncheckall: function() {        this.d_selected = false;      }    }  })
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>  <div id="app">    <div v-for="(val, key) in list">      <input type="radio" name="radio" :value="val" v-model="selected" :id="val">      <label :for="val">{{ val }}</label>    </div>    <button @click="uncheckall">uncheck all</button>  </div>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -