【发布时间】: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