[英]Form automatically submits when I don't want it to?
Every time I run any function using a button, the form gets automatically submitted, even though it shouldn't be submitted until the final button is pressed. 每次我使用按钮运行任何功能时,都会自动提交表单,即使在按下最后一个按钮之前也不应提交表单。 This doesn't happen outside the <form> tag. 这不是在<form>标记之外发生的。
Code: 码:
<div class = "form">
<form name = "contactForm" onsubmit="validateForm()">
<li>
<label>First name: </label><input type = "text" name = "fname" autofocus>
</li><br>
<li>
<label>Last Name: </label><input type = "text" name = "lname">
</li><br>
<li>
<label>Email: </label>
<input type = "text"name = "email">
<button onclick = "validateEmail()">Check if email is valid</button>
</li><br>
<li>
<label>Message: </label><br>
<textarea rows = "10" cols = "50" name = "message"></textarea>
</li>
<li>
<input type = "submit">
</li>
</form>
</ul>
The default behavior of a button
element is to submit the form in which it is contained. button
元素的默认行为是提交包含它的表单。 That is, the default value of its type
attribute is submit
. 也就是说,其type
属性的默认值是submit
。 To prevent a button
element from submitting automatically, set its type to button
, which has no default behavior. 为了防止button
元素自动提交,请将其类型设置为button
,这没有默认行为。
尝试这个..
<button onclick = "validateEmail();return false">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.