【问题标题】:Vue.js - Disable submit button unless original form data has changedVue.js - 除非原始表单数据已更改,否则禁用提交按钮
【发布时间】:2018-09-12 08:48:05
【问题描述】:

我有一个简单的表格,我只是为了实验目的而创建的。我试图保持按钮禁用,除非原始表单数据发生更改,但如果数据更改恢复为原始数据(撤消),仍保持按钮禁用。

<template lang="pug">
  form(@click.prevent="save")
    .main
      input(v-model="user.name")
      input(v-model="user.email")
      input(v-model="user.age")
      select(v-model="user.sex")
        option Male
        option Female
    .footer
      button(:disabled="isFormEnable") Save
</template>

<script>
export default {
  name: 'userForm',
  data () {
    return {
      user: {
        name: 'John Doe',
        email: 'john@gmail.com',
        age: '35',
        sex: 'Male',
      }
    }
  },

  computed: {
    isFormEnable () {
      // I am not sure what I need to do here but something like this may be:
      if (user.name) { return true }
    }
  },

  methods: {
    save () {
      console.log('Form Submitted')
    }
  }
}
</script>

我找到了一个 jQuery 解决方案 here,但我正在寻找 vanilla/vue javascript 解决方案。

$('form')
    .each(function(){
        $(this).data('serialized', $(this).serialize())
    })
    .on('change input', function(){
        $(this)             
            .find('input:submit, button:submit')
                .prop('disabled', $(this).serialize() == $(this).data('serialized'))
        ;
     })
    .find('input:submit, button:submit')
        .prop('disabled', true)
;

【问题讨论】:

  • 您可以尝试使用watched which watch properties change and reacts acc,而不是使用计算。对他们

标签: javascript vue.js vuejs2


【解决方案1】:

在 1 个模块的帮助下,我将这样做

npm i deep-diff

deep-diff 用于比较对象值。

<script>
import { diff } from "deep-diff";

// default form value
const defaultUser = {
  name: "John Doe",
  email: "john@gmail.com",
  age: "35",
  sex: "Male"
};

export default {
  //...
  data() {
    return {
      user: { ...defaultUser } // cloning the object using object spread syntax
    };
  },

  computed: {
    isFormEnable() {
      // check if it's default value
      if (!diff(this.user, defaultUser)) return false;

      return true;
    }
  },
  //...
};
</script>

【讨论】:

    【解决方案2】:

    以下是使用 Computed 和 Watch 属性本地执行此操作的方法:

    <template>
      <form>
        <label>Name</label>
        <input v-model='form.name' />
    
        <label>Age</label>
        <input v-model='form.age' />
    
        <button :disabled="!changed">Save</button>
      <form>
    </template>
    
    <script>
    import _ from 'lodash'
    
    export default {
      data() {
        return {
          changed: false, // for storing form change state
          form: {}, // data variable to store current form data binding
        }
      },
    
      computed: {
        // store the original form data
        originalForm() {
          return this.$store.state.form
        }
      },
      
      watch: {
        // by watching the original form data
        // create a clone of original form data
        // and assign it to the form data variable
        originalForm() {
          this.form = _.cloneDeep(this.originalForm)
        },
    
        // watch the changes on the form data variable
        form: {
          handler() {
            // using lodash to compare original form data and current form data
            this.changed = !_.isEqual(this.form, this.originalForm)
          },
          // useful to watch deeply nested properties on a data variable
          deep: true,
        },
      },
    
      created() {
        // dispatch an action to fill the store with data
        this.$store.dispatch('init')
      }
    }
    </script>
    

    Checkout this blog了解更多细节和更好的理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2019-07-19
      • 2020-04-23
      • 1970-01-01
      • 2016-04-03
      • 2012-06-19
      相关资源
      最近更新 更多