jquery 用于OTP输入字段仅当所有6位数字都已填充时才启用按钮

0s7z1bwu  于 2024-01-07  发布在  jQuery
关注(0)|答案(3)|浏览(126)

我有一个OTP表单字段,有一个禁用的“确认”按钮。该按钮应启用时,只有所有6位数字都填写。


的数据
超文本标记语言:

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

字符串
我已经尝试了下面的jQuery代码使用**.is(':empty')**,但它不工作.有人能指导我在这里我做错了什么。

  1. $('.form-otp').find('input').each(function() {
  2. $(this).on('keyup', function(e) {
  3. var parent = $($(this).parent());
  4. if(e.keyCode === 8 || e.keyCode === 37) {
  5. var prev = parent.find('input#' + $(this).data('previous'));
  6. if(prev.length) {
  7. $(prev).select();
  8. }
  9. } else if((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
  10. var next = parent.find('input#' + $(this).data('next'));
  11. if(next.length) {
  12. $(next).select();
  13. }
  14. }
  15. if ($(this).is(':empty')) {
  16. $('.js-otp-confirm').addClass('disabled');
  17. } else {
  18. $('.js-otp-confirm').removeClass('disabled');
  19. }
  20. });
  21. });

eoxn13cs

eoxn13cs1#

下面是我更新的脚本

  1. $('.form-otp').find('input').each(function() {
  2. $(this).on('keyup', function(e) {
  3. var parent = $($(this).parent());
  4. if (e.keyCode === 8 || e.keyCode === 37) {
  5. var prev = parent.find('input#' + $(this).data('previous'));
  6. if (prev.length) {
  7. $(prev).select();
  8. }
  9. } else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
  10. var next = parent.find('input#' + $(this).data('next'));
  11. if (next.length) {
  12. $(next).select();
  13. }
  14. }
  15. var counter = 0;
  16. $('.form-otp').find('input').each(function() {
  17. if ($(this).val() == '') {
  18. counter++;
  19. } else {
  20. }
  21. });
  22. if (counter > 0) {
  23. $('.js-otp-confirm').addClass('disabled');
  24. } else {
  25. $('.js-otp-confirm').removeClass('disabled');
  26. }
  27. });
  28. });

字符串

展开查看全部
ppcbkaq5

ppcbkaq52#

使用每个验证。

  1. $(document).ready(function(){
  2. var count = 0;
  3. $('.otp').change(function(){
  4. $('#otp input[type=text]').each(function(){
  5. if($(this).val().length !== 0) {
  6. count = count + 1;
  7. }
  8. });
  9. if(count === 21){
  10. $('#btn-active').removeClass('disabled');
  11. } else {
  12. $('#btn-active').addClass('disabled');
  13. }
  14. })
  15. });

个字符

展开查看全部
wbrvyc0a

wbrvyc0a3#

解决了1行过滤功能。

  1. $('.muse-otp').find('input').each(function () {
  2. $(this).on('keyup', function (e) {
  3. var parent = $($(this).parent());
  4. if (e.keyCode === 8 || e.keyCode === 37) {
  5. var prev = parent.find('input#' + $(this).data('previous'));
  6. if (prev.length) {
  7. $(prev).select();
  8. }
  9. } else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
  10. var next = parent.find('input#' + $(this).data('next'));
  11. if (next.length) {
  12. $(next).select();
  13. }
  14. }
  15. if ($('.muse-otp').find('input').filter(function () { return this.value === ''; }).length === 0) {
  16. $('.js-otp-confirm').removeClass('disabled');
  17. } else {
  18. $('.js-otp-confirm').addClass('disabled');
  19. }
  20. });
  21. });

字符串

展开查看全部

相关问题