vue生成一个form表单避免重复保存的代码
时间: 2024-05-19 21:15:40 浏览: 58
防止表单重复提交
以下是一个Vue生成一个form表单避免重复保存的代码:
<template>
<form @submit.prevent="onSubmit" v-if="!formSubmitted">
<label for="name">Name:</label>
<input type="text" id="name" v-model="form.name" required>
<label for="email">Email:</label>
<input type="email" id="email" v-model="form.email" required>
<button type="submit">Submit</button>
</form>
<div v-else>
<p>Form has been submitted. Thank you!</p>
</div>
</template>
<script>
export default {
data() {
return {
form: {
name: '',
email: ''
},
formSubmitted: false
}
},
methods: {
onSubmit() {
// Check if form has already been submitted
if (this.formSubmitted) {
return;
}
// Submit form data here
// ...
// Set formSubmitted to true to prevent duplicate submissions
this.formSubmitted = true;
}
}
}
</script>
阅读全文