繁体   English   中英

使用jQuery验证插件无法进行jQuery验证

[英]Jquery validation is not working using jquery validation plugin

我尝试使用jquery验证插件进行简单验证,但无法实现。单击“提交”按钮没有任何改变,请帮助我

我已经提到了以下代码,

提前致谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" />
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js" />
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnSubmit').click(function () { 
            $('#logform').validate({
                rules: {
                    txtUsr: {
                        required: true

                    },
                    txtPass: {
                        required: true

                    }

                },
                messages: {
                    txtUsr: {
                        required: 'Please enter username'
                    },
                    txtPass: {
                        required: 'Please enter password'
                    }
                }

            });

        });

        });

    </script>
</head>
<body>
<div id='LoginDiv' style='width:400px;height:300px;background-color:green;'>
    <form method="post" action="" id='logform'>
        User Name : <input type="text" id='txtUsr' name='txtUsr' style='margin-top:12px'/>
        <br/>
        <br/>
        Password : <input type="text" id='txtPass' name='txtPass' /> 
        <br/>
        <br/>
        <input type="submit" id='btnSubmit' value='Submit' />
    </form>
    </div>
</body>
</html>

您不应在click事件处理程序中调用validate,而应作为初始化步骤:

$(document).ready(function () {
    $('#logform').validate({
        rules: {
            txtUsr: {
                required: true
            },
            txtPass: {
                required: true
            }
        },
        messages: {
            txtUsr: {
                required: 'Please enter username'
            },
            txtPass: {
                required: 'Please enter password'
            }
        }
    });
});

它将自己连接到keyup并提交事件。

问题是由于脚本的自动关闭标记。

试试看:

http://jsfiddle.net/adiioo7/FFTkJ/

采用

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js"></script>

代替

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js" />

还要检查一下为什么自关闭脚本标签不起作用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM