宗策

导航

事件取消,表单提交事件

事件取消
      - 在事件中执行return false; 则可以将当前事件取消掉


表单提交事件 onsubmit
     - 当表单提交时触发

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
    span{
        visibility: hidden;
    }
    .ok{
        visibility: hidden;
    }
    .err{
        visibility: visible;
    }
</style>
<script type="text/javascript">
    function check1(){
        var i1 = document.getElementById("i1");
        var reg = /^\w{8,16}$/;
        s1.className = reg.test(i1.value)?"ok":"err";
        //如果校验成功返回true反之返回false
        return reg.test(i1.value)?true:false;
    }
    function check2(){
        var i2 = document.getElementById("i2");
        var reg = /^\w{8,16}$/;
        s2.className = reg.test(i2.value)?"ok":"err";
        return reg.test(i2.value)?true:false;
    }    
</script>
</head>
<body>
<a onclick="return confirm('确定要离开?');"
            href="http://baidu.com">超链接</a>
            
<form action="http://baidu.com"
         onsubmit="return check1()+check2()==2;">
    <input type="text" id="i1" placeholder="用户名"
        onblur="check1()">
    <span id="s1">错误格式,请重新输入</span><br>
    <input type="text" id="i2" placeholder="密码"
        onblur="check2()">
    <span id="s2">错误格式,请重新输入</span><br>
    <input type="submit"  value="登录"> 
</form>
</body>
</html>
View Code

 

posted on 2020-05-08 17:24  宗策  阅读(308)  评论(0编辑  收藏  举报