简体   繁体   English

表单的“动作”和“onsubmit”:哪个先执行?

[英]A form's “action” and “onsubmit”: Which executes first?

I'm trying to debug a webpage and I see a form element whose opening is 我正在尝试调试网页,我看到一个表单元素,其开头是

<form name="aspnetForm" method="post" action="default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">

Having only a base knowledge of web form s, I'm wondering what the order of execution is for the action and onsubmit . 由于只掌握了Web form的基本知识,我想知道对于actiononsubmit执行的顺序是什么。

If action was resolved first, then the browser would leave the page, the JS execution environment would go away, and there would be nowhere to run the JS in onsubmit , so it isn't. 如果首先解决了action ,那么浏览器将离开页面,JS执行环境就会消失,并且无法在onsubmit运行JS,所以事实并非如此。

Event handlers run before default actions. 事件处理程序在默认操作之前运行。 They can cancel default actions. 他们可以取消默认操作。

onsubmit 必须首先执行,因为从它返回false会停止提交的表单,从而执行所请求的action

This is explained in the HTML5 spec: 这在HTML5规范中有解释:

4.10.22.3 Form submission algorithm 4.10.22.3表单提交算法

When a form element form is submitted from an element submitter (typically a button), optionally with a submitted from submit() method flag set, the user agent must run the following steps: 当从元素提交者 (通常是按钮) 提交 form元素表单时 ,可选地submit()方法标志集submit() ,用户代理必须运行以下步骤:

  1. Let form document be the form 's Document . 表单文档成为表单Document

  2. If form document has no associated browsing context or its active sandboxing flag set has its sandboxed forms browsing context flag set, then abort these steps without doing anything. 如果表单文档没有关联的浏览上下文或其活动沙盒标志集设置沙箱形式浏览上下文标志 ,则中止这些步骤而不执行任何操作。

  3. Let form browsing context be the browsing context of form document . 表单浏览上下文成为表单文档浏览上下文

  4. If the submitted from submit() method flag is not set, and the submitter element's no-validate state is false, then interactively validate the constraints of form and examine the result: if the result is negative (the constraint validation concluded that there were invalid fields and probably informed the user of this) then fire a simple event named invalid at the form element and then abort these steps. 如果未设置submit()方法标志提交 ,并且submitter元素的无验证状态为false,则以交互方式验证 表单 的约束并检查结果:如果结果为负(约束验证结论为无效)字段,并可能通知用户这个)然后在表单元素上触发一个名为invalid 的简单事件 ,然后中止这些步骤。

  5. If the submitted from submit() method flag is not set, then fire a simple event that bubbles and is cancelable named submit , at form . 如果从提交的submit()方法标志没有设置,则触发一个简单事件的气泡,并取消命名为submit ,在形式 If the event's default action is prevented (ie if the event is canceled) then abort these steps. 如果阻止事件的默认操作(即,如果事件被取消),则中止这些步骤。 Otherwise, continue (effectively the default action is to perform the submission). 否则,继续(实际上默认操作是执行提交)。

  6. Let form data set be the result of constructing the form data set for form in the context of submitter . 表单数据集是在提交者的上下文中为表单 构造 表单数据集的结果。

  7. Let action be the submitter element's action . action成为提交者元素的动作

  8. If action is the empty string, let action be the document's address of the form document . 如果action是空字符串,则让action成为表单文档 的文档地址

  9. Resolve the URL action , relative to the submitter element. 相对于submitter元素解析 URL 操作 If this fails, abort these steps. 如果失败,请中止这些步骤。

  10. Let action be the resulting absolute URL . action成为最终的绝对URL

  11. Let action components be the resulting parsed URL . 动作组件成为生成的解析URL

  12. Let scheme be the scheme of the resulting parsed URL . scheme成为生成的解析URL方案

  13. Let enctype be the submitter element's enctype . enctypesubmitter元素的enctype

  14. Let method be the submitter element's method . 方法成为提交者元素的方法

  15. Let target be the submitter element's target . target成为submitter元素的目标

  16. If the user indicated a specific browsing context to use when submitting the form, then let target browsing context be that browsing context . 如果用户在提交表单时指示要使用的特定浏览上下文 ,则让目标浏览上下文成为该浏览上下文 Otherwise, apply the rules for choosing a browsing context given a browsing context name using target as the name and form browsing context as the context in which the algorithm is executed, and let target browsing context be the resulting browsing context . 否则,应用规则选择浏览上下文,给定浏览上下文名称,使用target作为名称,并将表单浏览上下文作为执行算法的上下文,并让目标浏览上下文成为生成的浏览上下文

  17. If target browsing context was created in the previous step, or, alternatively, if the form document has not yet completely loaded and the submitted from submit() method flag is set, then let replace be true. 如果在上一步中创建了目标浏览上下文 ,或者,如果表单文档尚未完全加载并且设置了submit()方法标志,那么让replace为true。 Otherwise, let it be false. 否则,就是假的。

  18. Otherwise, select the appropriate row in the table below based on the value of scheme as given by the first cell of each row. 否则,根据每行第一个单元格给出的scheme值,选择下表中的相应行。 Then, select the appropriate cell on that row based on the value of method as given in the first cell of each column. 然后,根据每列第一个单元格中给出的方法值选择该行上的相应单元格。 Then, jump to the steps named in that cell and defined below the table. 然后,跳转到该单元格中指定的步骤并在表格下方定义。

      | GET | POST ------------------------------------------------------- http | Mutate action URL | Submit as entity body https | Mutate action URL | Submit as entity body ftp | Get action URL | Get action URL javascript | Get action URL | Get action URL data | Get action URL | Post to data: mailto | Mail with headers | Mail as body 

    If scheme is not one of those listed in this table, then the behavior is not defined by this specification. 如果scheme不是此表中列出的那个之一,则此规范未定义该行为。 User agents should, in the absence of another specification defining this, act in a manner analogous to that defined in this specification for similar schemes. 在缺少定义此规范的另一规范的情况下,用户代理应以类似于本规范中针对类似方案定义的方式行事。

Therefore, at step 5, the submit is fired, and can be canceled to prevent form submission. 因此,在步骤5,触发submit ,并且可以取消提交以防止表单提交。 The action is resolved after that. 之后该行动得到解决。

onsubmit是为了检查格式等,然后第一次执行action ,执行获取/数据上传到后端。

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

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