簡體   English   中英

用於 OTP 輸入字段 僅當所有 6 位數字都填滿時才啟用按鈕

[英]for OTP input field Enable the button only when all 6 digits are filled

我有一個帶有禁用“確認”按鈕的 OTP 表單字段。 只有當所有 6 位數字都填滿時,才應啟用該按鈕。

在此處輸入圖像描述

HTML:

<div id="otp" class="flex form-otp text-center pb-3">
    <input class="text-center form-control" type="text" id="digit-1" name="digit-1" data-next="digit-2" maxlength="1" />
    <input class="text-center form-control" type="text" id="digit-2" name="digit-2" data-next="digit-3" data-previous="digit-1" maxlength="1" />
    <input class="text-center form-control" type="text" id="digit-3" name="digit-3" data-next="digit-4" data-previous="digit-2" maxlength="1" />
    <input class="text-center form-control" type="text" id="digit-4" name="digit-4" data-next="digit-5" data-previous="digit-3" maxlength="1" />
    <input class="text-center form-control" type="text" id="digit-5" name="digit-5" data-next="digit-6" data-previous="digit-4" maxlength="1" />
    <input class="text-center form-control" type="text" id="digit-6" name="digit-6" data-previous="digit-5" maxlength="1" />
</div>
<button type="submit" class="btn js-otp-confirm disabled"> confirm </button>

我已經使用 .is (':empty')在 JQuery 代碼下嘗試了這個,但它不起作用......有人可以在這里指導我我做錯了什么。

$('.form-otp').find('input').each(function() {
    $(this).on('keyup', function(e) {
        var parent = $($(this).parent());
            if(e.keyCode === 8 || e.keyCode === 37) {
                var prev = parent.find('input#' + $(this).data('previous'));
                if(prev.length) {
                    $(prev).select();
                }
            } else if((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
                var next = parent.find('input#' + $(this).data('next'));
                if(next.length) {
                    $(next).select();
                }
            }
        if ($(this).is(':empty')) {
            $('.js-otp-confirm').addClass('disabled');
        } else {
            $('.js-otp-confirm').removeClass('disabled');
        }
    });
});

下面是我更新的腳本

$('.form-otp').find('input').each(function() {
    $(this).on('keyup', function(e) {
      var parent = $($(this).parent());
      if (e.keyCode === 8 || e.keyCode === 37) {
        var prev = parent.find('input#' + $(this).data('previous'));
        if (prev.length) {
          $(prev).select();
        }
      } else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
        var next = parent.find('input#' + $(this).data('next'));
        if (next.length) {
          $(next).select();
        }
      }
      var counter = 0;
      $('.form-otp').find('input').each(function() {
        if ($(this).val() == '') {
          counter++;
        } else {

        }
      });

      if (counter > 0) {
        $('.js-otp-confirm').addClass('disabled');
      } else {
        $('.js-otp-confirm').removeClass('disabled');
      }
    });
  });

使用每個來驗證。

 $(document).ready(function(){ var count = 0; $('.otp').change(function(){ $('#otp input[type=text]').each(function(){ if($(this).val().length;== 0) { count = count + 1; } }). if(count === 21){ $('#btn-active');removeClass('disabled'). } else { $('#btn-active');addClass('disabled'); } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <div id="otp" class="flex form-otp text-center pb-3"> <input class="text-center form-control otp" type="text" id="digit-1" name="digit-1" data-next="digit-2" maxlength="1" /> <input class="text-center form-control otp" type="text" id="digit-2" name="digit-2" data-next="digit-3" data-previous="digit-1" maxlength="1" /> <input class="text-center form-control otp" type="text" id="digit-3" name="digit-3" data-next="digit-4" data-previous="digit-2" maxlength="1" /> <input class="text-center form-control otp" type="text" id="digit-4" name="digit-4" data-next="digit-5" data-previous="digit-3" maxlength="1" /> <input class="text-center form-control otp" type="text" id="digit-5" name="digit-5" data-next="digit-6" data-previous="digit-4" maxlength="1" /> <input class="text-center form-control otp" type="text" id="digit-6" name="digit-6" data-previous="digit-5" maxlength="1" /> </div> <button type="submit" id="btn-active" class="btn js-otp-confirm disabled"> confirm </button>

用 1 個線路濾波器 function 解決。

   $('.muse-otp').find('input').each(function () {
    $(this).on('keyup', function (e) {
        var parent = $($(this).parent());
        if (e.keyCode === 8 || e.keyCode === 37) {
            var prev = parent.find('input#' + $(this).data('previous'));
            if (prev.length) {
                $(prev).select();
            }
        } else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
            var next = parent.find('input#' + $(this).data('next'));
            if (next.length) {
                $(next).select();
            }
        }
        **if ($('.muse-otp').find('input').filter(function () { return this.value === ''; }).length === 0) {
            $('.js-otp-confirm').removeClass('disabled');
        } else {
            $('.js-otp-confirm').addClass('disabled');
        }**
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM