繁体   English   中英

jQuery - select function 不适用于移动设备

[英]jQuery - select function is not working on mobile

我做了一个OTP设计。 所以我有 6 个输入,每次用户输入一个数字时,它都会自动 go 到下一个输入。 我已经制作了下面的代码,但.select()不适用于手机。 有人可以帮我弄这个吗?

代码笔: https://codepen.io/aceraven777/pen/wvmbadQ

HTML

<div class="otp-group">
    <div class="otp-digit otp-digit-1">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-2">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-3">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-4">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-5">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-6">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>
</div>

JS

$('.otp-digit').each(function() {
    var $otp_digit = $(this);
    var $input = $otp_digit.find('input');

    $input.attr('maxlength', 1);
    $input.attr('pattern', '\\d*');

    $input.on('keyup', function(e) {
        var $input = $(this);
        var $otp_digit = $input.closest('.otp-digit');
        var $prev_otp_digit = $otp_digit.prev();
        var $next_otp_digit = $otp_digit.next();

        // Backspace or left arrow key
        if (e.keyCode === 8 || e.keyCode === 37) {
            if ($prev_otp_digit.length && $prev_otp_digit.hasClass('otp-digit')) {
                $prev_otp_digit.find('input').select().focus();
            }
        }
        // Right arrow and numbers
        else if (e.keyCode === 39 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
            if ($next_otp_digit.length && $next_otp_digit.hasClass('otp-digit')) {
                $next_otp_digit.find('input').select().focus();
            }
        }

        // Backspace
        if (e.keyCode === 8) {
            $input.val('');
        }
        else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
            var characterCode = ((96 <= e.keyCode && e.keyCode <= 105)? e.keyCode - 48 : e.keyCode);
            $input.val(String.fromCharCode(characterCode));
        } else {
            return false;
        }
    });

    $input.on('keypress', function(e) {
        var keyCode = e.keyCode || e.which;

        // Allow only typing numbers
        if (keyCode >= 48 && keyCode <= 57) {
            return true;
        }

        return false;
    });

    $input.on("change",function(e) {
        var $this = $(this);
        var input_value = $this.val().replace(/[^0-9]/gi, '');

        $this.val(input_value);
        
        var $form = $this.closest('.gigya-otp-login-form');
        var $inputs = $form.find('.otp-digit input');
        var otp_code = '';

        $inputs.each(function() {
            otp_code += $(this).val();
        });
    });

    $input.on("cut copy paste",function(e) {
        e.preventDefault();
    });
});

如果 jQuery .select()或将其分割成多个输入是不可能的,也许有人可以给我一个替代解决方案。

在这里分叉了您的代码并进行了一些更改。 以下是这些变化:

  1. 将输入类型文本更改为数字
  2. 添加 CSS 隐藏数字输入的箭头键。

JS 没有变化。

HTML

<div class="otp-group">
    <div class="otp-digit otp-digit-1">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-2">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-3">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-4">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-5">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-6">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>
</div>

额外 CSS

/* Add CSS to hide number arrows */
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

通过进行这些更改,我的移动设备开始显示数字键盘而不是全键盘,这对于 OTP 输入来说是更好的 UX,并且您的代码已经注意只允许数字输入,因此这是双赢的。

我不喜欢箭头键,所以我使用 CSS 将它们隐藏起来。 我不确定 CSS 是否适用于 iOS 设备,因为您可能知道 safari 不支持 Z2DDF56C37605420 中的很多东西

瞧。 它开始在我的手机上工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM