I am attempting to create a Vue instance with a group of radio buttons. My aim is that when a user clicks on a checked radio button, it will become unchecked. However, I have not been successful in accomplishing this using Vue so far. Below is the code I have written:
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;
}
}
})
It appears that the uncheck
method is being called, but then the radio button triggers a change
event and updates the value of
selected</code again. The <code>uncheckAll
method works as expected, probably because it does not use v-model
.
Do you have any suggestions or tips to make this functionality work? You can view an example of the code on this pen: https://codepen.io/diegoliv/pen/JrPBbG