【问题标题】:safari form validation works only in second button click, but works fine in IE 1st click itselfsafari 表单验证仅适用于第二次按钮单击,但在 IE 第一次单击本身中工作正常
【发布时间】:2014-07-31 12:51:44
【问题描述】:

我正在使用下面的 jquery 进行表单验证,下面的代码在 IE9 中运行良好,但在 safari 中有时它只能在第二次单击按钮时运行。

例如,

我正在输入正确的电子邮件和错误的密码,它显示错误消息,然后我更正密码并单击按钮,错误被删除但表单没有提交,然后我再次单击按钮,它正在提交。这仅发生在野生动物园中。我正在使用 Safari 5.1

我认为单个就绪函数就足够了,但我使用了 2 个就绪函数,我应该在不影响现有功能的情况下删除其中一个。

jQuery().ready(function domReady($) {
        $('#txtLogin_Id').removeClass('outLineRed');
        $('#txtPassword').removeClass('outLineRed');
        $('#spnLoginErr').hide();
        $('#txtLogin_Id').watermark("Email Address");
        $('#txtPassword').watermark("Password");

        if ($('#lblLoginErr').val() != '') {
            $('#spnLoginErr').show();
            $('#spnLoginErr').html($('#lblLoginErr').val());
            $('#MsgSucc').attr("style", "background-color:White;!important;");      
        }
        else {
            $('#spnLoginErr').hide();
            $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
        }

        $('#txtLogin_Id').blur(function () {
            if ($('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address.");
                $('#MsgSucc').attr("style", "background-color:White;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#spnLoginErr').hide();
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
                $('#txtLogin_Id').removeClass('outLineRed');
            }
        }
            );

        $('#txtPassword').blur(function () {
            if ($('#txtPassword').val() == '') {
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Password has to be more than 6 characters");
                $('#MsgSucc').attr("style", "background-color:White;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#spnLoginErr').hide();
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
                $('#txtPassword').removeClass('outLineRed');
            }
        }
            );

        $('#BtnLogin').click(function () {
            if ($('#txtPassword').val() == '' && $('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address and password.");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
            if ($('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address.");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#txtLogin_Id').removeClass('outLineRed');
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
            if ($('#txtPassword').val() == '') {
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Password has to be more than 6 characters");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#txtPassword').removeClass('outLineRed');
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
        });


        (function errorMsgBasedhighlight() {
            if ($('#lblLoginErr').val() == 'Please enter a valid email address and  password.') {
                $('#txtPassword').addClass('outLineRed');
                $('#txtLogin_Id').addClass('outLineRed');
            }
            else if ($('#lblLoginErr').val() == 'Please enter a valid email address.') {
                $('#txtLogin_Id').addClass('outLineRed');
            }
            else if ($('#lblLoginErr').val() == 'Please enter a valid Password.') {
                $('#txtPassword').addClass('outLineRed');
            }
            else {
                $('#txtLogin_Id').removeClass('outLineRed');
                $('#txtPassword').removeClass('outLineRed');
            }
        })($);

        $('#txtLogin_Id').blur(function () {
            if ($('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed'); 
                return false;
            }
            else {
                $('#lblLoginErr').hide();           
                $('#txtLogin_Id').removeClass('outLineRed');
            }
        }
            );
        $('#txtLogin_Id').blur(function () {
            if ($('#txtPassword').val() == '') {
                $('#txtPassword').addClass('outLineRed'); 
                return false;
            }
            else {
                $('#lblLoginErr').hide();           
                $('#txtPassword').removeClass('outLineRed');
            }
        }
            );
    });

【问题讨论】:

  • 我听说,如果我删除 jquery domready,我会工作得很好,是这样吗?我尝试删除它,但遇到了其他问题

标签: javascript jquery safari


【解决方案1】:

我自己发现了问题,这是因为同一个文本框的 2 个模糊函数并重写了 domReady

下面是代码

        $(document).ready{function(){ 
        $('#txtLogin_Id').removeClass('outLineRed');
        $('#txtPassword').removeClass('outLineRed');
        $('#spnLoginErr').hide();
        $('#txtLogin_Id').watermark("Email Address");
        $('#txtPassword').watermark("Password");

        if ($('#lblLoginErr').val() != '') {
            $('#spnLoginErr').show();
            $('#spnLoginErr').html($('#lblLoginErr').val());
            $('#MsgSucc').attr("style", "background-color:White;!important;");      
        }
        else {
            $('#spnLoginErr').hide();
            $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
        }

        $('#txtLogin_Id').blur(function () {
            if ($('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address.");
                $('#MsgSucc').attr("style", "background-color:White;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#spnLoginErr').hide();
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
                $('#txtLogin_Id').removeClass('outLineRed');
            }
        }
            );

        $('#txtPassword').blur(function () {
            if ($('#txtPassword').val() == '') {
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Password has to be more than 6 characters");
                $('#MsgSucc').attr("style", "background-color:White;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#spnLoginErr').hide();
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
                $('#txtPassword').removeClass('outLineRed');
            }
        }
            );

        $('#BtnLogin').click(function () {
            if ($('#txtPassword').val() == '' && $('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address and password.");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
            if ($('#txtLogin_Id').val() == '') {
                $('#txtLogin_Id').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Please enter email address.");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#txtLogin_Id').removeClass('outLineRed');
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
            if ($('#txtPassword').val() == '') {
                $('#txtPassword').addClass('outLineRed');
                $('#spnLoginErr').show();
                $('#spnLoginErr').html("Password has to be more than 6 characters");
                $('#MsgSucc').attr("style", "background-color:White;!important;");
                $("html, body").animate({ scrollTop: 0 }, "slow");
                return false;
            }
            else {
                $('#txtPassword').removeClass('outLineRed');
                $('#MsgSucc').attr("style", "background-color:#dfe5e6;");
            }
        });


        (function errorMsgBasedhighlight() {
            if ($('#lblLoginErr').val() == 'Please enter a valid email address and  password.') {
                $('#txtPassword').addClass('outLineRed');
                $('#txtLogin_Id').addClass('outLineRed');
            }
            else if ($('#lblLoginErr').val() == 'Please enter a valid email address.') {
                $('#txtLogin_Id').addClass('outLineRed');
            }
            else if ($('#lblLoginErr').val() == 'Please enter a valid Password.') {
                $('#txtPassword').addClass('outLineRed');
            }
            else {
                $('#txtLogin_Id').removeClass('outLineRed');
                $('#txtPassword').removeClass('outLineRed');
            }
        })($);


            });

【讨论】:

    【解决方案2】:

    使用

    $( document ).ready(function() {
    

    而不是

    jQuery().ready(function domReady($) {
    

    并写好文档之外的所有事件。

    【讨论】:

    • 我试过了,可能是我遗漏了什么,你能发布一些例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多