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

后端 未结 6 716
一向
一向 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 01:57
      $("#create-form").validate({ // Set Focus on first invalid input
        focusInvalid: false,
        invalidHandler: function() {
          $(this).find(":input.error:first").focus();
          }
      });
    
    0 讨论(0)
  • 2021-01-30 01:59

    Maybe you could check what input failed and take it's position (top) and use jQuery's scrollTop

    $(window).scrollTop(errorPosition)
    

    It seems that getting each error field isn't very easy to get (at least for me).

    Search for errorPlacement in the Validation plugin documentation. There is an example how to get each error field.

    0 讨论(0)
  • 2021-01-30 02:01

    Here's what you can do:

    • By default the validate plugin focuses the first erroneous element (in case there's any). Turn off the option focusInvalid by setting it to false.

    • The callback invalidHandler handler is executed when the form is invalid. You get access through the second parameter validator to the validator object and thus to the errorList array. You can then animate the scroll relatively to the first erroneous element.

    Here's the code:

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

    DEMO

    0 讨论(0)
  • 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;
      }
    });
    
    0 讨论(0)
  • 2021-01-30 02:16

    I dont like all the jQuery extentions so here is my solution to this problem:

    if ($('#MYID').valid()) {
          //dosomething();
    } else {
        $('html, body').animate({
             scrollTop: ($('.error').offset().top - 300)
        }, 2000);
    }
    
    0 讨论(0)
  • 2021-01-30 02:16

    just add this code to your themes javascript:

    (function($) {
    $(document).ready(function(){
        //bmo scroll to not valid
        $(".wpcf7").on('invalid.wpcf7',function(e){
            $('html, body').animate({
                    scrollTop: $(".wpcf7-not-valid").first().offset().top-30
                }, 2000);
        });
    
    });
    })(jQuery);
    
    0 讨论(0)
提交回复
热议问题