js阻止form表单提交

<form id="login" class="form-horizontal" action="login_login.action" onsubmit="return validate();" method="post">

</form>

function validate(){
        alert("请输入验证码!"); //则弹出请输入验证码  
        return false;
    }
    else if(inputCode != code ) {
        alert("验证码输入错误!@_@"); //则弹出验证码输入错误  
        createCode();//刷新验证码  
        document.getElementById("verificatCode").value = "";//清空文本框  
        return false;
    }
    else {//输入正确时
        return true;
    }
}

在form表单中onsubmit的书写一定是要"return 方法()",只写"方法()"则不行。并且要有返回值,返回false,就会阻止提交,返回true,则不会。错误的写法是:

<form id="login" class="form-horizontal" action="login_login.action" onsubmit="validate();" method="post">

</form>


猜你喜欢

转载自blog.csdn.net/a1062656171/article/details/75174603