表单提交时,点击功能的顺序并提交 [英] when form submit ,the order of click function and submit

查看:106
本文介绍了表单提交时,点击功能的顺序并提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于点击并提交
示例如下:

about click and submit example below:

<form action="url" method="post">
<input type="text" id="input1"/>
<input type="submit" value="submit" onclick="testFun()"/>
</form>

如果我们点击按钮提交时表单提交后可能运行testFun函数

if it is possible that function testFun run after the form's submit when we click the button to submit

如果答案是否定的。
为什么?单击提交按钮时浏览器如何工作?订单是点击功能 - >提交?是对吗?

if the answser is no. why? how does the browser work when click the submit button?? the order is click function-> submit ? is right??

推荐答案

在表单提交后,你不能执行一个函数 - 执行的顺序是如下:

No you cannot execute a function after the form has been submitted - the order of which things are executed is as follows :


  • 用户点击提交按钮

  • onclick 函数已执行

  • 浏览器将页面提交到表单操作中指定的 url

  • User clicks the submit button
  • The onclick function is executed
  • The browser submits the page to the url specified in the action of the form

您可以通过从onclick处理程序返回 false 来阻止浏览器提交页面:

You can prevent the browser submitting the page by returning false from the onclick handler :

function myfunc() {
  // do some stuff
  return false;
}

然后应该像这样修改提交按钮:

the submit button should then be modified like this :

<input type="submit" onclick="return myfunc()"/>

如果您希望在提交表单后执行某项功能,则需要使用表单提交表单AJAX - 这不会导致浏览器离开页面,并且在表单提交后可以执行JavaScript函数

If you do wish to execute a function after the form has been submitted you need to submit the form using AJAX - this doesnt cause the browser to navigate away from the page and a JavaScript function can be executed after the form has been submitted

这篇关于表单提交时,点击功能的顺序并提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆