Ad
How Can I Disable A Link In The Vue Component?
My html like this :
<div id="app">
<a class="btn btn-danger" target="_blank" rel="nofollow noreferrer" href="javascript:" @click="add($event)">add</a>
</div>
My javascript like this :
var vue = new Vue({
el: '#app',
methods: {
add(event) {
event.target.disabled = true
}
}
});
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?
Ad
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:
new Vue({
el: '#app',
methods: {
add(event) {
event.target.className += ' disabled'
}
}
})
body { padding: 10px }
<script src="https://unpkg.com/vue"></script>
<link rel="stylesheet" type="text/css" target="_blank" rel="nofollow noreferrer" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div id="app">
<a class="btn btn-danger" target="_blank" rel="nofollow noreferrer" href="javascript:" @click.prevent="add">add</a>
</div>
You can also go pure Vue and use a class binding:
new Vue({
el: '#app',
data: {
btnDisabled: false
},
methods: {
add(event) {
this.btnDisabled = true; // mutate data and let vue disable the element
}
}
})
body { padding: 10px }
<script src="https://unpkg.com/vue"></script>
<link rel="stylesheet" type="text/css" target="_blank" rel="nofollow noreferrer" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div id="app">
<a class="btn btn-danger" target="_blank" rel="nofollow noreferrer" href="javascript:" @click.prevent="add" :class="{disabled: btnDisabled}">add</a>
</div>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad