【问题标题】:jQuery "scroll-to-top" where error message appears in form validationjQuery“滚动到顶部”,其中错误消息出现在表单验证中
【发布时间】:2017-06-21 13:28:10
【问题描述】:

我正在尝试让这个脚本工作:

function scrollToAnchor(aid)
{
    if($(".error").length > 0) 
    { 
        var firstErrorElement = $(".error").first(); 

        $('html,body').animate({scrollTop:firstErrorElement.offset().top},'slow'); 
    }                                                 

    var aTag = $("a[name='"+ aid +"']");
    $('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

请看看我要做什么here

【问题讨论】:

  • 抱歉,这里有问题吗?
  • 有谁知道这段代码的问题出在哪里?
  • 函数 scrollToAnchor(aid){ if($(".error").length > 0) { var firstErrorElement = $(".error").first(); $('html,body').animate({scrollTop:firstErrorElement.offset().top},'slow'); } var aTag = $("a[name='"+ 辅助 +"']"); $('html,body').animate({scrollTop: aTag.offset().top},'slow'); }

标签: jquery validationerror


【解决方案1】:

已通过以下方式解决:https://stackoverflow.com/users/123127/didier-ghys http://jsfiddle.net/didierg/Kwhbv/

$("#SurveyForm").validate({
    focusInvalid: false,
    invalidHandler: function(form, validator) {
        
        if (!validator.numberOfInvalids())
            return;
        
        $('html, body').animate({
            scrollTop: $(validator.errorList[0].element).offset().top
        }, 1000);
        
    }
});

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多