To keep track of the disabled state of all dinosaurs, you can create a new property:
data() {
disabled: {
Triceratops: true,
Velociraptor: true,
Tyrannosaurus: true,
},
}
Next, connect the button's disabled
attribute to this property:
<button :disabled="disabled[dino]">Click me</button>
In the method enableButton(dino)
, you can remove the disabled state for the specified dinosaur:
enableButton(dino) {
this.disabled[dino] = false;
}
new Vue({
el: '#app',
data: () => ({
dinos: [
"Triceratops", "Velociraptor", "Tyrannosaurus"
],
disabled: {
Triceratops: true,
Velociraptor: true,
Tyrannosaurus: true,
},
}),
methods: {
enableButton(dino) {
this.disabled[dino] = false;
},
onClick(dino) {
alert(dino)
}
}
});
button:disabled {
color: gray;
}
<script src="https://unpkg.com/vue"></script>
<div id="app">
<template v-for="dino in dinos">
<div>
<input type="text" :value="dino" @input="enableButton(dino)">
<button @click="onClick(dino)" :disabled="disabled[dino]">Click me</button>
</div>
</template>
</div>