【问题标题】:Avoid scrolling top when validate condition is not meet [duplicate]当验证条件不满足时避免滚动顶部[重复]
【发布时间】:2018-12-22 13:17:49
【问题描述】:

我正在将 jquery-steps 用于带有 manu 必填字段的长表单。我担心的是,每次条件失败时,页面都会滚动到顶部。

特别烦人的是“条款和条件”页面很长,如果用户不勾选“我同意”复选框,它必须到底部重试。

这是我的简单代码:

var form = $("#myform");
form.validate({
   errorPlacement: function errorPlacement(error, element) {
      if (element.attr("name") == "type") {
         error.appendTo($('#errorbox'));
      }
      else {
         element.before(error);
      }
   },
   rules: {
      cgu: {
         required: true
      }
   },
   messages: {
      cgu: {
         required: 'Vous devez accepter les C.G.U pour poursuivre'
      }
   },
   focusInvalid: true,
   onfocusout: function (element) {
      $(element).valid();
   },
});

即使使用“focusInvalid: true”,页面仍会滚动到顶部。

【问题讨论】:

  • 您向我们展示的代码中没有任何内容可以解释任何滚动。 focusInvalid: true 是默认行为,因此将其放在那里毫无意义。请向我们展示重现问题的完整代码示例。

标签: jquery jquery-validate jquery-steps


【解决方案1】:

已使用此解决方法解决:

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

【讨论】:

  • 究竟解决了什么?您的 OP 缺少描述和代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2021-10-27
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多