表单会在我不想要时自动提交?

时间:2014-04-23 14:50:13

标签: javascript forms

每次使用按钮运行任何功能时,表单都会自动提交,即使在按下最后一个按钮之前也不会提交。这不会发生在< 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>

2 个答案:

答案 0 :(得分:2)

button元素的default behavior是提交包含它的表单。也就是说,其type属性的默认值为submit。要阻止button元素自动提交,请将其类型设置为button,它没有默认行为。

答案 1 :(得分:-1)

试试这个..

<button onclick = "validateEmail();return false">