简体   繁体   English

如果未选择至少一个文件,则阻止提交表单

[英]Prevent submitting a form if at least one file is not selected

I am using Vue.js and Axios.我正在使用 Vue.js 和 Axios。

I have two inputs on my html page for selecting an image.我的 html 页面上有两个用于选择图像的输入。 There is also a form.还有一种形式。 The two inputs and the form are separate and independent.两个输入和表单是分开的和独立的。

When I select an image, the image is automatically uploaded to the server.当我选择图像时,图像会自动上传到服务器。

The challenge is how to make it impossible to submit the form until at least one image is selected?挑战是如何在至少选择一张图像之前无法提交表单?

Tell me how to do this?告诉我怎么做?

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Add Item</title>
        <script src="https://unpkg.com/vue@next"></script>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    </head>
    <body>

    <div id="app">

        <form name="addItem" method="post" action="/user/addItem">
            <label for="name">name</label>
            <input type="text" id="name">
            <button type="submit" id="submit">Save Item</button>
        </form>



        <form @submit.prevent enctype="multipart/form-data">
            <input type="file" ref="file1" @change="selectFile1">
        </form>

        <form @submit.prevent enctype="multipart/form-data">
            <input type="file" ref="file2" @change="selectFile2">
        </form>



    </div>


    <script>

        const addProduct = {

            name: "addProduct",

            data() {
                return{
                    file1: "",
                    image1:"",

                    file2: "",
                    image2: "",
                }
            },

            methods:{

                async selectFile1(){
                    const formData = new FormData();
                    this.file1 = this.$refs.file1.files[0];
                    formData.append('file', this.file1);
                    await axios.post('/user/uploadImage', formData)
                },

                async selectFile2(){
                    const formData = new FormData();
                    this.file2 = this.$refs.file2.files[0];
                    formData.append('file', this.file2);
                    await axios.post('/user/uploadImage', formData)
                }

            }
        }

        Vue.createApp(addProduct).mount('#app')
    </script>


    </body>
    </html>
<form name="addItem" method="post" action="/user/addItem">
    <label for="name">name</label>
    <input type="text" id="name">
    <button type="submit" @click="submitForm" id="submit">Save Item</button>
</form>

<script>
    ...
    submitForm(e) {
        if (!this.file1 && !this.file2) e.preventDefault()
    }
    ...
</script>

By default your form submit button is disabled until any one of the file has been selected默认情况下,您的表单submit按钮处于disabled状态,直到选择了任何一个文件

Step 1: HTML template like第 1 步: HTML模板,如

 <template>
  <div id="app">
    <form name="addItem" @submit.prevent="submit">
      <div class="row">
        <label for="name">Name</label>&nbsp;
        <input type="text" id="name" />
      </div>
      <div class="row">
        <input type="file" ref="file1" @change="selectFile1" />
      </div>
      <div class="row">
        <input type="file" ref="file2" @change="selectFile2" />
      </div>
      <div class="row">
        <button type="submit" id="submit" :disabled="!this.file1 && !this.file2">Save Item</button>&nbsp;
        <button type="button" @click.prevent="reset">Reset</button>
      </div>
    </form>
  </div>
</template>

Step 2: methods like第 2 步: methods

 methods: {
  submit() {
    if (!this.file1 && !this.file2) {
      return false;
    } else {
      const savedata = {
        name: this.name,
      };
      axios.post("/user/addItem", savedata);
    }
  },
  async selectFile1() {
    const formData = new FormData();
    this.file1 = this.$refs.file1.files[0];
    formData.append("file", this.file1);
    await axios.post("/user/uploadImage", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
  async selectFile2() {
    const formData = new FormData();
    this.file2 = this.$refs.file2.files[0];
    formData.append("file", this.file2);
    await axios.post("/user/uploadImage", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
  reset() {
    this.name = "";
    this.$refs.file1.value = null;
    this.$refs.file2.value = null;
    this.file1 = "";
    this.file2 = "";
  },
},

DEMO Link演示链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保在提交多页表单之前至少选中了一个复选框? - How to make sure at least 1 checkbox is selected before submitting multipage form? 有没有一种方法可以防止用户在至少移动滑块(输入类型=范围)之前提交表单? - Is there a way to prevent the user from submitting form before s/he has at least moved the slider (input type = range)? 防止表单提交 - Prevent form from submitting 是否需要添加功能以确保提交多页表单之前至少选中了一个复选框? - Need to add function to make sure at least 1 checkbox is selected before submitting multipage form? 一次提交表单并下载文件 - Submitting a form and downloading a file in one action 检查表单是否至少有一个填充/选中/选定的元素 - Check if form has at least one filled / checked / selected element HTML 表单验证 - 检查是否至少选择了一个下拉列表 - HTML form validation - check if at least one dropdown is selected 如何使用表单验证 jquery 检查至少一个选中的复选框? - How to check at least one selected checkbox using form validation jquery? 表格是在输入类型=文件中没有选择文件的提交时间 - Form is timeouting on submitting without selected file in input type=file 出现错误时阻止表单提交 - Prevent Form from Submitting if there are Errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM