JQuery表单的非空验证

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-3.1.1.js"></script>
    <script type="text/javascript">
        $(function () {
            var m1 = true;
            var m2 = true;
            if ($(".aa").val() =="") {
                m1 = false;
            }
            if ($(".bb").val() =="") {
                m2 = false;
            }

            $("#submit").click(function(){
                if (m1 && m2) {
                    $('form').submit();
                } else {
                    alert("用户名或者密码不能为空");
                    return false;
                }
            });

        });
//        $(function(){
//            $("#submit").click(function(){
//                alert("用户名不能为空");
//            });
//        });


    </script>
</head>
<body>
<form action="http://www.w3cschool.cn/jquery/jquery-hide-show.html" method="post">
    <table align="center">
        <tr>
            <td>请输入用户名:</td>
            <td><input type="text" NAME="username" class="aa"></td>
        </tr>

        <tr>
            <td>请输入密码:</td>
            <td><input type="password" NAME="pwd" class="bb"></td>
        </tr>

        <tr>
            <td colspan="2"><input type="submit" id="submit" name="submit" value="提   交"></td>
        </tr>
    </table>
</form>
</body>
</html>
posted @ 2017-04-03 14:44  猿老大  阅读(2261)  评论(0编辑  收藏  举报