【问题标题】:Error: jQuery jquery.min.js?_=1420285687057:4 Uncaught TypeError: undefined is not a function错误:jQuery jquery.min.js?_=1420285687057:4 Uncaught TypeError: undefined is not a function
【发布时间】:2015-01-03 12:45:53
【问题描述】:

当我点击几次时,我遇到了 jquery validate 插件和历史 API 的问题 我有错误 jquery.min.js?_=1420288991396:4 Uncaught TypeError: undefined is not a function,当我用 validate 注释片段时,脚本工作正常。如何连接这个脚本。 在文件 script.js 我有:

$(document).ready(function(){

    $("#loginForm").validate({
            rules: {
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
            },
            messages: {
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
            },

            submitHandler: function() {
                var username=$("#usernameLogin").val();
                var password=$("#passwordLogin").val();
                var dataString = 'username='+username+'&password='+password+'&tag=login';
                if($.trim(username).length>0 && $.trim(password).length>0){
                    $.ajax({
                        type: "POST",
                        url: "include/help4meApi.php",
                        data: dataString,
                        cache: false,
                        success: function(data){
                            if(data){       
                                if($("#rememberMe:checked").val()){
                                    checkCookie(username);
                                }
                                window.location.href = "index.php";
                            }
                            else{   
                                $("#error").html("<label class='error'>Wrong password or username!</label>");
                                console.log("Wrong password or username!");
                            }
                        }
                    });
                }
                return false;
            }
        });


        // validate signup form on keyup and submit
        $("#signupForm").validate({
            rules: {
                firstname: {
                    required: true,
                    minlength: 3
                },
                lastname: "required",
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                },
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                firstname: {
                    required: "Please enter your firstname",
                    minlength: "Your firstname  must consist of at least 3 characters"
                },
                lastname: "Please enter your lastname",
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                },
                email: "Please enter a valid email address",
            },

            submitHandler: function() {
                var username=$("#username").val();
                var password=$("#password").val();
                var firstname=$("#firstname").val();
                var lastname=$("#lastname").val();
                var email=$("#email").val();
                var dataString = 'username='+username+'&password='+password+'&firstname='+firstname+'&lastname='+lastname+'&email='+email+'&tag=register';
                if($.trim(username).length>0 && $.trim(password).length>0){
                    $.ajax({
                        type: "POST",
                        url: "include/help4meApi.php",
                        data: dataString,
                        cache: false,
                        success: function(data){
                            if(data){
                                console.log(data);
                                $(".register_notification").html("<label class='error' style='background:#6DC066'>Register successfully!</label>");
                            }
                            else{   
                                $(".register_notification").html("<label class='error'>Username already exists, please choose another one!</label>");
                            }
                        }
                    });
                }
                return false;
            }
        });

        // propose username by combining first- and lastname
        $("#username").focus(function() {
            var firstname = $("#firstname").val();
            var lastname = $("#lastname").val();
            if (firstname && lastname && !this.value) {
                this.value = firstname + "." + lastname;
            }
        });

    $("footer a").click(function(e){
        //e.preventDefault(); 
        /*  
        if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
        if commented, html5 nonsupported browers will reload the page to the specified link. 
        */

        //get the link location that was clicked
        pageurl = $(this).attr('href');
        //to get the ajax content and display in div with id 'content'
        $.ajax({url:pageurl,success: function(data){
            $('#login_box').html(data);
        }});

        //to change the browser URL to 'pageurl'
        if(pageurl!=window.location){
            window.history.pushState({path:pageurl},'',pageurl);    
        }
        return false;  
    });
 });


$(window).bind('popstate', function() {
    if(!location.pathname.localeCompare('/login.php')){
        $.ajax({url:location.pathname,success: function(data){
            $('body').html(data);
        }});
    }
    else{
        $.ajax({url:location.pathname,success: function(data){
            $('#login_box').html(data);
        }});
    }

});

来自 jQuery 库:

// Get transport
transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );

// If no transport, we auto-abort
if ( !transport ) {
    done( -1, "No Transport" );
} else {
    jqXHR.readyState = 1;

    // Send global event
    if ( fireGlobals ) {
        globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
    }
    // Timeout
    if ( s.async && s.timeout > 0 ) {
        timeoutTimer = setTimeout(function() {
            jqXHR.abort("timeout");
        }, s.timeout );
    }

    try {
        state = 1;
        transport.send( requestHeaders, done );
    } catch ( e ) {
        // Propagate exception as error if not done
        if ( state < 2 ) {
            done( -1, e );
        // Simply rethrow otherwise
        } else {
            throw e;
        }
    }
}

它显示了 throw e。

【问题讨论】:

  • 您确定重现您的问题需要所有这些代码吗?
  • 你能说得更具体点吗?哪一行抛出错误?
  • @Vohuman 那就是问题所在,行号是:jquery.min.js?_=1420288991396:4
  • 为什么要使用没有源映射的缩小库进行开发?
  • @Vohuman,继续 help4me.pl/login.php 点击 Kontakt 链接并点击退格键,再次执行此操作,如果您查看控制台,则出现错误

标签: javascript jquery jquery-validate html5-history


【解决方案1】:

我想不出使用浏览器的“后退”按钮会导致 JavaScript 失败的任何原因。一切都应该被缓存并像以前一样继续工作。

您遇到的错误类似于您未能包含插件时遇到的错误。

以下是包含 jQuery Validate 插件的方式...

<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>

注意您是如何热链接到托管在开发者自己的域上的插件的。 (可能是他的演示和/或下载。)这个错误可能是由时间问题引起的,插件从这个 URL 加载的速度不够快。

开发者在两个不同的免费 CDN 服务上提供了他的插件,其中“欢迎热链接”。因此,不要直接从他的域中提取插件,而是尝试热链接到提供的 CDN 链接here

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    相关资源
    最近更新 更多