每次使用按钮运行任何功能时,表单都会自动提交,即使在按下最后一个按钮之前也不会提交。这不会发生在< form>之外。标签
代码:
<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>
答案 0 :(得分:2)
button
元素的default behavior是提交包含它的表单。也就是说,其type
属性的默认值为submit
。要阻止button
元素自动提交,请将其类型设置为button
,它没有默认行为。
答案 1 :(得分:-1)
试试这个..
<button onclick = "validateEmail();return false">