简体   繁体   中英

Toggle input element's disabled attribute depending on a checkbox value using VueJS

I'm trying to create a form in which one text input field will have disabled attribute if a checkbox is checked. I tried:

 new Vue({ el: "#app", data: { isChecked: false, name: null } }) 
 <script src="https://cdn.jsdelivr.net/npm/vue"></script> <div id="app"> <input type="text" v-model="name" :class="{disabled: isChecked}" placeholder="disabled if box checked"> <input type="checkbox" v-model="isChecked"> </div> 

Instead if using a conditional class, you can just use the property disabled and make it dynamic by adding a : in front of it and binding it to the isChecked variable. Your code will then look like this :

<div id="app">
  <input type="text" v-model="name" :disabled="isChecked" placeholder="disabled if box checked">
  <input type="checkbox" v-model="isChecked">
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM