How to disable input conditionally in vue.js

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

I have an input:



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

    To toggle the input's disabled attribute was surprisingly complex. The issue for me was twofold:

    (1) Remember: the input's "disabled" attribute is NOT a Boolean attribute.
    The mere presence of the attribute means that the input is disabled.

    However, the Vue.js creators have prepared this... https://vuejs.org/v2/guide/syntax.html#Attributes

    (Thanks to @connexo for this... How to add disabled attribute in input text in vuejs?)

    (2) In addition, there was a DOM timing re-rendering issue that I was having. The DOM was not updating when I tried to toggle back.

    Upon certain situations, "the component will not re-render immediately. It will update in the next 'tick.'"

    From Vue.js docs: https://vuejs.org/v2/guide/reactivity.html

    The solution was to use:

    this.$nextTick(()=>{
        this.disableInputBool = true
    })
    

    Fuller example workflow:

    <div @click="allowInputOverwrite">
        <input
            type="text"
            :disabled="disableInputBool">
    </div>
    
    <button @click="disallowInputOverwrite">
        press me (do stuff in method, then disable input bool again)
    </button>
    
    <script>
    
    export default {
      data() {
        return {
          disableInputBool: true
        }
      },
      methods: {
        allowInputOverwrite(){
          this.disableInputBool = false
        },
        disallowInputOverwrite(){
          // accomplish other stuff here.
          this.$nextTick(()=>{
            this.disableInputBool = true
          })
        }
      }
    
    }
    </script>
    
    0 讨论(0)
  • 2020-12-07 10:40

    Can use this add condition.

      <el-form-item :label="Amount ($)" style="width:100%"  >
                <template slot-scope="scoped">
                <el-input-number v-model="listQuery.refAmount" :disabled="(rowData.status !== 1 ) === true" ></el-input-number>
                </template>
              </el-form-item>
    
    0 讨论(0)
  • 2020-12-07 10:41

    You may make a computed property and enable/disable any form type according to its value.

    <template>
        <button class="btn btn-default" :disabled="clickable">Click me</button>
    </template>
    <script>
         export default{
              computed: {
                  clickable() {
                      // if something
                      return true;
                  }
              }
         }
    </script>
    
    0 讨论(0)
  • 2020-12-07 10:47

    you could have a computed property that returns a boolean dependent on whatever criteria you need.

    <input type="text" :disabled=isDisabled>
    

    then put your logic in a computed property...

    computed: {
      isDisabled() {
        // evaluate whatever you need to determine disabled here...
        return this.form.validated;
      }
    }
    
    0 讨论(0)
  • 2020-12-07 10:50

    Try this

     <div id="app">
      <p>
        <label for='terms'>
          <input id='terms' type='checkbox' v-model='terms' /> Click me to enable
        </label>
      </p>
      <input :disabled='isDisabled'></input>
    </div>
    

    vue js

    new Vue({
      el: '#app',
      data: {
        terms: false
      },
      computed: {
        isDisabled: function(){
            return !this.terms;
        }
      }
    })
    
    0 讨论(0)
提交回复
热议问题