需求:当表单中未填写任何项时,提交按钮置灰,只要填写其中一项,则可点击按钮。
<el-button type="primary" :disabled="isButtonGray" @click="handleSave('ruleForm')"> 保存 </el-button>
data() {
return {
formItem: {
commodityCode: null,
name: '',
price: ''
},
preForm: null
}
},
watch: {
resetData: function() {
this.$refs.ruleForm.resetFields()
},
formItem: {
handler:function(nowVal,oldVal){
var $this = this;
for(let i in $this.formItem){
if(nowVal[i] != $this.preForm[i]) {
$this.isButtonGray = false;
break;
}else {
$this.isButtonGray = true;
}
}
},
deep:true
}
},
mounted() {
this.preForm = Object.assign({}, this.formItem)
},
本文介绍了一种使用Vue.js实现的表单验证方法,当表单为空时,提交按钮将被置灰不可用,一旦输入任意字段,按钮立即激活。通过监听表单项的变化,实现了按钮状态的实时更新。
7296

被折叠的 条评论
为什么被折叠?



