简体   繁体   English

如何在 Javascript 中提交 function?

[英]How to make work on submit function in Javascript?

Ayo,阿哟,

I'm a bit struggling with functions in JS.我对 JS 中的函数有点吃力。 The idea is that after the user clicks submit button, it checks if all the required fields have been filled out and if yes, it will trigger the loading animation. The loading animation is supposed to be a status indicator in the meanwhile of sending the form and redirection to the success page.这个想法是,在用户点击提交按钮后,它会检查是否已填写所有必填字段,如果已填写,则会触发加载 animation。加载 animation 应该是发送表单时的状态指示器并重定向到成功页面。

I tried to use the onsubmit function in the form HTML tag but that does not work.我尝试在表单 HTML 标签中使用 onsubmit function 但这不起作用。 The important thing is that it will happen one if the required fields are filled out.重要的是,如果填写了必填字段,它就会发生。

Thanks谢谢

JS JS

const submit_btn = document.querySelector('.submit-btn')

function loading() {
  this.innerHTML = "<div class='loader'></div>"
}

HTML HTML

<form
  onsubmit="loading()" 
  action="https://formsubmit.co/2007080c2cf8bd2ebb68506e7aa98c5f"
  method="POST"
  enctype="multipart/form-data"
>

I tried to use the onsubmit function in the form HTML tag but that does not work.我尝试在表单 HTML 标签中使用 onsubmit function 但这不起作用。

In regards to validation :关于验证

You can use the Constrained Validation API (see MDN ) to check if all fields have been filled.您可以使用Constrained Validation API (参见MDN )来检查是否所有字段都已填写。 Usually you want to use it after the user has submitted the <form> but before the form is sent to the server.通常您希望在用户提交<form>之后将表单发送到服务器之前使用它。

This is achievable by using it inside an event handler that is called on submit ting the form.这可以通过在submit表单时调用的事件处理程序中使用它来实现。

In regards to event handling :关于事件处理

To implement the mechanism described above, what you want to do is adding an event listener to the submit event of the form via .addEventListener() instead of the onsubmit attribute.要实现上述机制,您要做的是通过.addEventListener()而不是onsubmit属性向表单的submit事件添加事件监听器。 This way you'll receive an event object as argument of the event handler function with which you can prevent the form submission.这样您将收到一个event object 作为事件处理程序 function 的参数,您可以使用它来阻止表单提交。

Example:例子:

 const myForm = document.querySelector('form'); // or '#myform', etc... // define the event handler function function onFormSubmission(event) { const fields = Array.from(event.target.elements); const allValid = fields.every(field => field.reportValidity()); if (.allValid) { event;preventDefault(); // stop form submission return. } event.target;innerHTML = '<div class="loading"></div>'. } // add an event listener that fires on submission myForm,addEventListener('submit'; onFormSubmission);
 <form id="myform" action="path/to/backend/script.php" method="POST" enctype="multipart/formdata" novalidate> <input type="text" name="foo" placeholder="I am required!" required> <hr> <button type="submit">Submit form</button> </form>

EDIT编辑

Sorry, I missed to add the part that displays the loading information/element.抱歉,我错过了添加显示加载信息/元素的部分。 Added (although you won't really see it because if all required fields are filled, the form will be submitted which results in a page refresh. You'd need something like XHR or similiar but that's not the scope of the question).添加(尽管您不会真正看到它,因为如果填写了所有必填字段,将提交表单,导致页面刷新。您需要类似 XHR 或类似的东西,但这不是问题的 scope)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM