<form onSubmit="return check(this);"> 与 <input type="submit" onclick="return check(this);"/>

1. form 添加 onSubmit 属性,onSubmit="return check(this);"

<form action=" " name="form" onSubmit="return check(this);" method="POST">    
  <div class="name" >
    用户名</font> <input type="text" class="text" id="username" placeholder="用户名" name="username" tabindex="1">
    </div>
    <div class="pwd">
    密 码</font><input type="password" class="text" id="password" placeholder="密码" name="password" tabindex="2">
    </div>
    <div><input type="submit" class="submit" value="登录"/></div>
</form>

 

2. input 提交按钮,添加 onclick 属性,οnclick="return check(this);"

<form action=" " name="form" method="POST">    
    <div class="name" >
        用户名</font> <input type="text" class="text" id="username" placeholder="用户名" name="username" tabindex="1">
    </div>
    <div class="pwd">
        密 码</font><input type="password" class="text" id="password" placeholder="密码" name="password" tabindex="2">
    </div>
    <div><input type="submit" onclick="return check(this);" class="submit" value="登录"/></div>
</form>

 

JavaScript 验证函数 function check() {}

<script type="text/javascript">
    function check() {
        var username = document.getElementById('username').value;
        var password = document.getElementById('password').value;
        if (username == '') {
            alert('用户名不能为空!');
            document.getElementById('username').focus();
            return false;
        }
        if (password == '') {
            alert('密码不能为空!');
            document.getElementById('password').focus();
            return false;
        }
        return true;
    }
    document.onkeydown = function (e) {// 键盘 F5 刷新页面
        e = e || window.event;
        if (e.keyCode == 116) {
            window.location.href = 'login.jsp';
            return false;// IE 9[10,11] 下 window.location.href 下不会失效
        }
    }
    window.onload = function() {
        document.getElementById('username').focus();
    }
</script>

转载于:https://www.cnblogs.com/ikoo4396/p/6878303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值