My html like this :
JavaScript
x
4
1
<div id="app">
2
<a class="btn btn-danger" href="javascript:" @click="add($event)">add</a>
3
</div>
4
My javascript like this :
JavaScript
1
10
10
1
var vue = new Vue({
2
el: '#app',
3
4
methods: {
5
add(event) {
6
event.target.disabled = true
7
}
8
}
9
});
10
Demo and full code like this : https://jsfiddle.net/q7xcbuxd/221/
I try like that. But if I click button add, it’s not disabled
How can I solve this problem?
Advertisement
Answer
Since you are using boostrap, the proper way to disable a (anchor) button is not to set .disabled = true
, but to add a disabled
class.
Two other notes. You probably want to prevent the default behavior of the click
event, so use @click.prevent
. Also, if you don’t have additional arguments, you don’t need to use ="add($event)"
, just ="add"
will suffice.
Demo below:
JavaScript
1
8
1
new Vue({
2
el: '#app',
3
methods: {
4
add(event) {
5
event.target.className += ' disabled'
6
}
7
}
8
})
JavaScript
1
1
1
body { padding: 10px }
JavaScript
1
6
1
<script src="https://unpkg.com/vue"></script>
2
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
3
4
<div id="app">
5
<a class="btn btn-danger" href="javascript:" @click.prevent="add">add</a>
6
</div>
You can also go pure Vue and use a class binding:
JavaScript
1
11
11
1
new Vue({
2
el: '#app',
3
data: {
4
btnDisabled: false
5
},
6
methods: {
7
add(event) {
8
this.btnDisabled = true; // mutate data and let vue disable the element
9
}
10
}
11
})
JavaScript
1
1
1
body { padding: 10px }
JavaScript
1
6
1
<script src="https://unpkg.com/vue"></script>
2
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
3
4
<div id="app">
5
<a class="btn btn-danger" href="javascript:" @click.prevent="add" :class="{disabled: btnDisabled}">add</a>
6
</div>