When form is validated, how to SCROLL to the first error instead of jumping?

后端 未结 6 732
一向
一向 2021-01-30 01:34

I\'ve seen many questions with variations on this theme, but I\'m looking for the straightforward solution:

HTML form, jQuery validation, multiple fields are required. W

6条回答
  •  暖寄归人
    2021-01-30 02:15

    try this:

    $( "input[type=submit]" ).click(function() {
        event.preventDefault();
        event.stopPropagation();
        //  console.log("test")
        var errorElements = document.querySelectorAll(".input-validation-error");
      for (let index = 0; index < errorElements.length; index++) {
          const element = errorElements[index];
        //  console.log(element);
          $('html, body').animate({
            scrollTop: $(errorElements[0]).focus().offset().top - 25
          }, 1000);      
          return false;
      }
    });
    

提交回复
热议问题