3

以下脚本将在未聚焦时显示错误消息。我将其更改为 jQuery 4.3 或更高版本,但它没有这样做。我知道我在做一些愚蠢的事情,但找不到。请提供建议。谢谢

<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>jQuery validation</title>
        <script src="http://code.jquery.com/jquery-1.4.2.js"></script> 
        <script src="jquery.validate.js" type="text/javascript"></script>
        <script src="additional-methods.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function(){
                var validate = $("#signupForm").validate({
                    rules: {phone:'phoneUS'},
                    messages: {phone:'Enter a valid phone number'},
                    submitHandler: function(form) {alert('submit');}
                });

            });
        </script>
    </head>
    <body>

        <form id="signupForm" method="get" action="">
            <label for="phone">phone</label>
            <input id="phone" name="phone" />
            <input type="submit" value="Submit" class="submit" />
        </form>
    </body>
</html>
4

1 回答 1

2

phoneUS方法定义在additional-methods.js. 如果您包含此文件(并确保您的输入有type='text'),它应该可以正常工作:

JavaScript:

$("#signupForm").validate({
    rules: {
        phone: 'phoneUS'
    },
    messages: {
        phone: 'Enter a valid phone number'
    },
    submitHandler: function(form) {
        alert('submit');
    }
});

HTML:

<form id="signupForm" method="get" action="">
    <label for="phone">phone</label>
    <input id="phone" name="phone" type="text"/>
    <input type="submit" value="Submit" class="submit" />
</form>​​​​​​​​​​​​​​​

示例:http: //jsfiddle.net/kzDnx/5/

于 2012-08-24T19:20:50.037 回答