javascript - 看不到我正在制作的 inf 循环

标签 javascript jquery jquery-plugins

对于学校作业,我必须制作一个 jQuery 插件。我几乎完成了它,但由于某种原因我真的看不到,当我尝试提交表单时,我最终陷入了无限循环(这会导致 Google Chrome 崩溃)。这是代码:

console.log("starting plugin");
$(document).ready(init);

function init() {
    $('#password').on('keyup', clickHandler);
}
function clickHandler() {
    console.log("clickHandler");
    $.fn.passwordCheck();

}
(function () {
    var settings = {
        passwordInput: $('input[type="password"]'),
        passwordNeededLength: 8,
        passwordStrenght: 2,
        errorMessageLoc: 'passwordinfomessage',
        messageLoc: '#passinfo'
    };
    var methods = {
        init: function () {
            console.log("started methods init");
            methods.checkStrength();
        },
        createMessages: function (status) {
            console.log("start function createmessages");
            var $messageLoc = $(settings.messageLoc);
            var $errormessage = settings.errorMessageLoc;
            $($messageLoc).css("display","inline-block");
            if (status === 1) {
                $("#passinfo").empty();
                $messageLoc.append("<p id='passwordLength' class=" + $errormessage + ">the password needs to be atleast " + settings.passwordNeededLength + " characters</p>");
                console.log("created message #1");
            } else if (status === 2) {
                $("#passinfo").empty();
                $($messageLoc).append("<p id='weak' class=" + $errormessage + ">The password is to weak to be accepted, please try adding some characters</p>");
                console.log("created message #2");
            } else if (status === 3) {
                $("#passinfo").empty();
                $($messageLoc).append("<p id='good' class=" + $errormessage + ">The password is the password is of good strength, u can try adding some numbers and capital letters</p>");
                console.log("created message #3");
            } else if (status === 4) {
                $("#passinfo").empty();
                $($messageLoc).append("<p id='strong' class=" + $errormessage + ">The password is strong</p>");
                console.log("created message #4");
            } else if (status === 5) {
                $("#passinfo").empty();
                $($messageLoc).append("<p id='submitButtonMessage' class=" + $errormessage + ">Something is wrong with your password</p>");
                console.log("created message #5");
            }
        },
        checkStrength: function () {
            console.log("start function checkStrength");
            var strength = 0;
            if (settings.passwordInput.val().length >= settings.passwordNeededLength) {
                strength += 1;
                console.log("password length is accept");
                if (settings.passwordInput.val().length >= 25) strength += 2;

                if (settings.passwordInput.val().match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1;

                if (settings.passwordInput.val().match(/([a-zA-Z])/) && settings.passwordInput.val().match(/([0-9])/))  strength += 1;

                if (settings.passwordInput.val().match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1;

                if (settings.passwordInput.val().match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1;

                methods.checkStrengthResult(strength);
            }
            else {
                console.log(settings.passwordInput.val().length);
                console.log("not big");
                if (settings.passwordInput.val() == 0) {
                    console.log("just entered the site");

                } else {
                    $('.' + settings.errorMessageLoc).remove();
                    methods.createMessages(1);
                }
            }
        },
        checkStrengthResult: function (strength) {
            console.log("start function checkStrengthResult");
            console.log(strength);
            //if value is less than 2
            if (strength < 2) {
                console.log("password is to weak");
                methods.createMessages(2);
            }
            if (strength == 2) {
                console.log("password is good");
                methods.createMessages(3);
            }
            if (strength > 3) {
                console.log("password is strong");
                methods.createMessages(4);
                methods.checkPasswordStrength(strength)
            }
        }
        ,
        checkPasswordStrength: function (strength) {
            $( "#testform" ).submit(function( event ) {
                console.log( "Handler for .submit() called." );
                if (strength >= settings.passwordStrenght&& settings.passwordInput.val().length >= settings.passwordNeededLength){
                    console.log("is good");
                    console.log("i will submit");
                    $( "#testform" ).submit();
                    event.preventDefault();

                } else{
                    console.log("not good");
                    event.preventDefault();
                    methods.init();
                }
            });

//            console.log("stopt posting");
//            if (typeof strength == 'undefined'|| strength == 0) {
//                event.preventDefault();
//                console.log("strength was undifined");
//                methods.init()
//            } else {
//                if (strength >= settings.passwordStrenght&& settings.passwordInput.val().length >= settings.passwordNeededLength) {
//                    console.log("password is strong enhough");
//                    event.preventDefault();
//                } else {
//                    console.log("stoping submitting");
////                  console.log(strength);
//                    event.preventDefault();
//                    methods.createMessages(5);
//
//                }
//            }
////        }
        }
    };
    $.fn.passwordCheck = function (options) {
        console.log("start password check");
        if (options) {
            settings = $.extend(settings, options);
            console.log(options);
            console.log(settings);
        }

        $('#password').on('keyup', methods.init());
        $('#submitbutton').on('click', methods.checkPasswordStrength());

        return this;
    };
})(jQuery);

在最后一种方法中,我之前尝试过一些方法,但我没有得到有效的以太币。

最佳答案

您必须提交,然后在处理程序中再次提交表单。

改变

$( "#testform").submit();

$( "#testform")[0].submit();

这将执行 native 表单提交,而不是再次触发提交事件。

关于javascript - 看不到我正在制作的 inf 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015018/

相关文章:

javascript - 在函数中调用窗口对象

javascript - 具有相同功能的多能ID选择器

jquery - 可滚动获取元素的当前值

javascript - 使用 jQuery 制作 PNG 动画?

jquery - 如何以不引人注目的方式使用 propHooks?

javascript - 使用 Javascript 使用 "Select Name"填充表单值

javascript - Firefox 和 Chrome 中的 IndexedDB 错误 : Unknown error and DOMException

javascript - 使用 Promise.all 执行数组中的 promise 会破坏它们

jquery - CSS 和 jQuery slider 高度/定位问题

javascript - 介绍 : when using intro background images appear above popup