iOS Safari/Chrome不会向上滚动以显示无线电输入的验证错误消息

taor4pac  于 2023-03-14  发布在  iOS
关注(0)|答案(4)|浏览(106)

我正在为需要手机友好的客户做一个调查问卷。
当表单被提交/验证,而一个问题没有得到回答时,它会滚动到第一个无效的字段-无论是文本输入还是单选输入。这只适用于桌面浏览器和Android。
在iOS Safari/Chrome上,文本输入工作正常,就像在桌面浏览器和Android上一样。
但对于无线电输入,它关注尚未回答的问题并显示验证错误消息,但如果当前显示屏中没有焦点问题,则不会向上滚动到焦点问题。
This question has been asked,但从未得到答复。是否有解决方法?

<form name="myForm" id="myForm"method="post">  
            <div class="form-row" id="info">
                <div class="col-md-3 mb-3">
                    <label for="name">Name</label>
                    <input type="text" class="form-control" id="name" placeholder="Name" value="Tony Stark" required>
                </div> <!-- NAME -->
                <div class="col-md-3 mb-3">
                    <label for="company">Company</label>
                    <input type="text" class="form-control" id="company" placeholder="Company" value="Stark Industries">
                </div> <!-- COMPANY -->
                <div class="col-md-3 mb-3">
                    <label for="address">Address</label>
                    <input type="text" class="form-control" id="address" placeholder="Address" value="10880 Malibu Point" required>
                </div> <!-- ADDRESS -->
                <div class="col-md-3 mb-3">
                    <label for="address2">Apt / Suite / Other (Optional)</label>
                    <input type="text" class="form-control" id="address2" placeholder="Apt / Suite / Other">
                </div> <!-- ADDRESS2 -->
            </div>

            <div class="form-row" id="citystzip">
                <div class="col-md-6 mb-3">
                    <label for="city">City</label>
                    <input type="text" class="form-control" id="city" placeholder="City" value="Malibu" required>
                </div> <!-- CITY -->

                <div class="col-md-3 mb-3">
                    <label for="state">State</label>
                    <select class="form-control" id="state" placeholder="State" value="CA" required>
                        <option value="AL">AL</option>
                        <option value="AK">AK</option>
                        <option value="AZ">AZ</option>
                        <option value="AR">AR</option>
                        <option value="CA">CA</option>
                        <option value="CO">CO</option>
                        <option value="CT">CT</option>
                        <option value="DE">DE</option>
                        <option value="DC">DC</option>
                        <option value="FL">FL</option>
                        <option value="GA">GA</option>
                        <option value="HI">HI</option>
                        <option value="ID">ID</option>
                        <option value="IL">IL</option>
                        <option value="IN">IN</option>
                        <option value="IA">IA</option>
                        <option value="KS">KS</option>
                        <option value="KY">KY</option>
                        <option value="LA">LA</option>
                        <option value="ME">ME</option>
                        <option value="MD">MD</option>
                        <option value="MA">MA</option>
                        <option value="MI">MI</option>
                        <option value="MN">MN</option>
                        <option value="MS">MS</option>
                        <option value="MO">MO</option>
                        <option value="MT">MT</option>
                        <option value="NE">NE</option>
                        <option value="NV">NV</option>
                        <option value="NH">NH</option>
                        <option value="NJ">NJ</option>
                        <option value="NM">NM</option>
                        <option value="NY">NY</option>
                        <option value="NC">NC</option>
                        <option value="ND">ND</option>
                        <option value="OH">OH</option>
                        <option value="OK">OK</option>
                        <option value="OR">OR</option>
                        <option value="PA">PA</option>
                        <option value="RI">RI</option>
                        <option value="SC">SC</option>
                        <option value="SD">SD</option>
                        <option value="TN">TN</option>
                        <option value="TX">TX</option>
                        <option value="UT">UT</option>
                        <option value="VT">VT</option>
                        <option value="VA">VA</option>
                        <option value="WA">WA</option>
                        <option value="WV">WV</option>
                        <option value="WI">WI</option>
                        <option value="WY">WY</option>
                    </select>
                </div> <!-- STATE -->
                <div class="col-md-3 mb-3">
                    <label for="zip">Zip</label>
                    <input type="text" class="form-control" id="zip" placeholder="Zip" value="90265" required>
                </div> <!-- ZIP -->
            </div>

            <div class="form-row" id="contact">
                <div class="col-md-7 mb-3">
                    <label for="email">Email</label>
                    <input type="email" class="form-control" id="email" placeholder="Email" value="tony@starkindustries.com" required>
                </div> <!-- EMAIL -->

                <div class="col-md-5 mb-3">
                    <label for="phone">Phone Number</label>
                    <input type="tel" class="form-control" id="phone" placeholder="Phone Number" value="678-136-7092" required>
                </div> <!-- PHONE -->
            </div>

        <h1>Please answer all survey questions and click submit at the bottom to submit <br>your updated information and survey answers.</h1>
            <legend>1. Question 1</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation1" name="question1" value="1a" required>
                <label class="custom-control-label" for="customControlValidation1">1a</label>
            </div>
            <div class="custom-control custom-radio mb-3">
                <input type="radio" class="custom-control-input" id="customControlValidation2" name="question1" value="1b" required>
                <label class="custom-control-label" for="customControlValidation2">1b</label>
            </div>

            <legend>2. Question 2</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation3" name="question2"  value="2a" required>
                <label class="custom-control-label" for="customControlValidation3">2a</label>
            </div>
            <div class="custom-control custom-radio mb-3">
                <input type="radio" class="custom-control-input" id="customControlValidation4" name="question2" value="2b" required>
                <label class="custom-control-label" for="customControlValidation4">2b</label>
            </div>

            <legend>3. Question 3</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation5" name="question3" value="3a" required>
                <label class="custom-control-label" for="customControlValidation5">3a</label>
            </div>
            <div class="custom-control custom-radio mb-3">
                <input type="radio" class="custom-control-input" id="customControlValidation6" name="question3" value="3b" required>
                <label class="custom-control-label" for="customControlValidation6">3b</label>
            </div>

            <legend>4. Question 4</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation7" name="question4" value="4a" required>
                <label class="custom-control-label" for="customControlValidation7">4a</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation8" name="question4" value="4b" required>
                <label class="custom-control-label" for="customControlValidation8">4b</label>
            </div>

            <legend>5. Question 5</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation9" name="question5" value="5a" required>
                <label class="custom-control-label" for="customControlValidation9">5a</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation10" name="question5" value="5b" required>
                <label class="custom-control-label" for="customControlValidation10">5b</label>
            </div>

            <legend>6. Question 6</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation11" name="question6" value="6a" required>
                <label class="custom-control-label" for="customControlValidation11">6a</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation12" name="question6" value="6b" required>
                <label class="custom-control-label" for="customControlValidation12">6b</label>
            </div>

            <legend>7. Question 7</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation13" name="question7" value="7a" required>
                <label class="custom-control-label" for="customControlValidation13">7a</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation14" name="question7" value="7b" required>
                <label class="custom-control-label" for="customControlValidation14">7b</label>
            </div>

            <legend>8. Question 8</legend>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation15" name="question8" value="8a" required>
                <label class="custom-control-label" for="customControlValidation15">8a</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="customControlValidation16" name="question8" value="8b" required>
                <label class="custom-control-label" for="customControlValidation16">8b</label>
            </div>
ni65a41a

ni65a41a1#

由于OP仍在寻找变通方案,而我也在积极寻找解决方案,以下是我目前的变通方案:
首先我测试是否使用iPhone/iPad(请检查下面的注解),然后将EventListener连接到监视焦点事件的单选框和复选框输入。由于焦点被正确设置在第一个无效输入上,剩下要做的唯一事情就是将该输入滚动到可见区域。

if (/^iPad|iPhone$/.test(navigator.platform)) {
    var inputs = form.querySelectorAll('input[type="radio"], input[type="checkbox"]');
    inputs.forEach(i => {
        i.addEventListener('focus', function (e) {
            e.target.scrollIntoView({
                block: 'center',
                behavior: "smooth"
            });
        })
    })
}
  • 注 *:由于iPadOS (/^iPad|iPhone$/.test(navigator.platform))似乎不再工作。Tell iPadOS from macOS on the web列出了解决方案,但由于我目前没有能够运行iPadOS的iPad,我还不能测试它们。
rbpvctlc

rbpvctlc2#

通过JQuery,您可以使用
$.scrollTo($('#myDiv'), 1000)

vcudknz3

vcudknz33#

<input onFocus={(e) => {
                                                                document.getElementsByName(e.target.name)[0].scrollIntoView({
                                                                    behavior: 'smooth',
                                                                    block: 'center',
                                                                    inline: 'start',
                                                                });
                                                            }}
gwbalxhn

gwbalxhn4#

这是我在不影响其他表单行为的情况下做的一个变通方法。由于验证失败时单选元素将被聚焦,所以如果单选元素不在视口中,则滚动到它。

/// fix iOS not auto scroll to radio input when validation fail
function isElementInViewport(el) {
    //https://stackoverflow.com/questions/123999/how-can-i-tell-if-a-dom-element-is-visible-in-the-current-viewport
    // Special bonus for those using jQuery
    if (typeof jQuery === "function" && el instanceof jQuery) {
        el = el[0];
    }

    var rect = el.getBoundingClientRect();

    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        /* or $(window).height() */
        rect.right <= (window.innerWidth || document.documentElement
            .clientWidth) /* or $(window).width() */
    );
}
//change form-check-input to your radio input class
$(".form-check-input").focus(function() {
    if (!isElementInViewport(this)) {
        document.getElementById(this.id).scrollIntoView();
    }
});

相关问题