简体   繁体   English

提交功能不会触发onsubmit返回功能吗?

[英]submit function does not trigger onsubmit return function?

I think I have a misunderstanding of the onsubmit attribute. 我认为我对onsubmit属性有误解。 I thought that every submission, like a <input type="submit" ...> , <button></button> or any javascript form.submit() trigger the onsubmit="return function();" 我认为每次提交,例如<input type="submit" ...><button></button>或任何JavaScript form.submit()触发onsubmit="return function();" after the submit. 提交之后。 In other words, I will never see the "last triggered" log. 换句话说,我将永远不会看到“上次触发”日志。

Example: 例:

 function triggerFirst() { console.log("first triggered"); myForm.submit(); } function triggerLast() { console.log("last triggered"); return true; } 
 <form id="myForm" onsubmit="return triggerLast();"> <input type="button" onclick="triggerFirst();" value="trigger"> </form> 

This example will never trigger the onsubmit, why? 这个例子永远不会触发onsubmit,为什么? I thought onsubmit means if someone submits? 我认为onsubmit表示是否有人提交? Is that false? 那是假的吗?

It works. 有用。

UPDATED: Place the 2nd function inside first. 更新:将第二个函数放在里面。

 function triggerFirst() { alert('wow'); triggerLast() } function triggerLast() { alert('wowpsubmit'); return true; } 
 <form id="myForm" onsubmit='return triggerLast();'> <input type="button" onclick="triggerFirst();" value="trigger" > </form> 

An HTML form does not require javascript to work. HTML表单不需要javascript即可工作。 Refer the code below: 请参考以下代码:

<form action="action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey"><br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit" value="Submit">
</form>

The <form action="action_page.php"> declaration is run once the form is submitted. 提交表单后,将运行<form action="action_page.php">声明。

The <input type="submit" value="Submit"> is a built in type that triggers the form action to run. <input type="submit" value="Submit">是一种内置类型,可触发表单操作运行。

See more here 在这里查看更多

In your triggerFirst function the inner function call to submit does nothing. 在您的triggerFirst函数中,内部函数提交没有任何作用。 That submit function is not defined. 该提交功能未定义。 I think here you want to make the function submit so below I passed a reference to the form into the triggerFirst function. 我想在这里您要使函数提交,因此下面我将对表单的引用传递给triggerFirst函数。 This should clarify things for you. 这应该为您澄清事情。 Open the console to view the output. 打开控制台以查看输出。

 function triggerFirst(form) { console.log('triggerFirst') console.log(form); form.submit(); } function triggerLast() { console.log('triggerLast') return true; } 
 <form onsubmit="return triggerLast();"> <input type="button" onclick="triggerFirst(this.form);" value="trigger"> </form> 

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

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