HTML5 input required, scroll to input with fixed navbar on submit

前端 未结 9 2056
感情败类
感情败类 2020-12-08 09:35

When trying to submit a form with missing required fields, my browser (Chrome), displays a message mentionning there is a field missing, and if it\'s out of my screen, it sc

9条回答
  •  独厮守ぢ
    2020-12-08 09:40

    ok, i did a dirty test with a code snippet i found here on SO

    As it is a code from someone else, i just alter it to scroll to the element that had a missing input requirement. I do not want any credit for it, and it maybe is not even what you have in mind, you or someone else could use it as a reference.

    The goal was to get the id of the forgotten/wrong input element:

                var myelement = input.id;
                var el = document.getElementById(myelement);
                el.scrollIntoView(false);
    

    Please keep in mind that this fiddle only works for your posted fiddle above, it not handles multiple forgotten or wrong input fields.I only wanted to show an alternative.

    ----->jSFiddle

提交回复
热议问题