How to disable input conditionally in vue.js

后端 未结 11 1227
余生分开走
余生分开走 2020-12-07 10:11

I have an input:



        
相关标签:
11条回答
  • 2020-12-07 10:24

    Bear in mind that ES6 Sets/Maps don't appear to be reactive as far as i can tell, at time of writing.

    0 讨论(0)
  • 2020-12-07 10:28

    To remove the disabled prop, you should set its value to false. This needs to be the boolean value for false, not the string 'false'.

    So, if the value for validated is either a 1 or a 0, then conditionally set the disabled prop based off that value. E.g.:

    <input type="text" :disabled="validated == 1">
    

    Here is an example.

    var app = new Vue({
      el: '#app',
    
      data: {
        disabled: 0,
      },
    }); 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
      <button @click="disabled = (disabled + 1) % 2">Toggle Enable</button>
      <input type="text" :disabled="disabled == 1">
        
      <pre>{{ $data }}</pre>
    </div>

    0 讨论(0)
  • 2020-12-07 10:30

    Your disabled attribute requires a boolean value:

    <input :disabled="validated" />

    Notice how i've only checked if validated - This should work as 0 is falsey ...e.g

    0 is considered to be false in JS (like undefined or null)

    1 is in fact considered to be true

    To be extra careful try: <input :disabled="!!validated" />

    This double negation turns the falsey or truthy value of 0 or 1 to false or true

    don't believe me? go into your console and type !!0 or !!1 :-)

    Also, to make sure your number 1 or 0 are definitely coming through as a Number and not the String '1' or '0' pre-pend the value you are checking with a + e.g <input :disabled="!!+validated"/> this turns a string of a number into a Number e.g

    +1 = 1 +'1' = 1 Like David Morrow said above you could put your conditional logic into a method - this gives you more readable code - just return out of the method the condition you wish to check.

    0 讨论(0)
  • 2020-12-07 10:33

    This will also work

    <input type="text" id="name" class="form-control" name="name"  v-model="form.name" :disabled="!validated">
    
    0 讨论(0)
  • 2020-12-07 10:36

    Not difficult, check this.

    <button @click="disabled = !disabled">Toggle Enable</button>
    <input type="text" id="name" class="form-control" name="name"  v-model="form.name" :disabled="disabled">
    

    jsfiddle

    0 讨论(0)
  • 2020-12-07 10:38

    You can manipulate :disabled attribute in vue.js.

    It will accept a boolean, if it's true, then the input gets disabled, otherwise it will be enabled...

    Something like structured like below in your case for example:

    <input type="text" id="name" class="form-control" name="name" v-model="form.name" :disabled="validated ? false : true">
    

    Also read this below:

    Conditionally Disabling Input Elements via JavaScript Expression

    You can conditionally disable input elements inline with a JavaScript expression. This compact approach provides a quick way to apply simple conditional logic. For example, if you only needed to check the length of the password, you may consider doing something like this.

    <h3>Change Your Password</h3>
    <div class="form-group">
      <label for="newPassword">Please choose a new password</label>
      <input type="password" class="form-control" id="newPassword" placeholder="Password" v-model="newPassword">
    </div>
    
    <div class="form-group">
      <label for="confirmPassword">Please confirm your new password</label>
      <input type="password" class="form-control" id="confirmPassword" placeholder="Password" v-model="confirmPassword" v-bind:disabled="newPassword.length === 0 ? true : false">
    </div>
    
    0 讨论(0)
提交回复
热议问题