javascript 当我们按capslock键时,如何显示capslock打开错误消息?

e5nszbig  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(102)

当我们按键盘上的capslock键时,如何显示capslock是错误信息?它与注解部分一起工作,实际上我需要像未注解的输入密码一样的标签!如果我们按capslock键,即使不按,我也只能显示错误信息,不需要做任何事情!

<div class="form-group">
        <label for="password">Password<span class="star"> *</span></label>
            <div class="input-group">
               <div class="input-group-prepend bg-transparent">
                    <span class="caps-lock-warning" style="color: brown;">caps lock is on.</span>
                    <i class="fa fa-lock text-dark"></i>
                    </span>
               </div>
                <input type="password" class="form-control form-control-lg border-left-0" id="password" placeholder="Password" name="password" required autocomplete="off">
                </div>
            </div>
    <!-- <div>
        <input type="password" id="password" />
        <span class="caps-lock-warning" style="color: brown;">caps lock is on.</span>
    </div>
     -->
<script>
    $(function(){
    $('.caps-lock-warning').hide();
  
    $("#password").keypress(function(e) {
      var s = String.fromCharCode( e.which );
      if((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey)||
         (s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)) {
        this.caps = true; 
        $(this).next('.caps-lock-warning').show();
      } else if((s.toLowerCase() === s && s.toUpperCase() !== s && !e.shiftKey)||
                (s.toLowerCase() !== s && s.toUpperCase() === s && e.shiftKey)) {
        this.caps = false; 
        $(this).next('.caps-lock-warning').hide();
      }
    });
  
    $(document).keydown(function(e){
      if(e.which==20){
        var pass = document.getElementById("password");
        if(typeof(pass.caps) === 'boolean'){
          pass.caps = !pass.caps;
          $(pass).next('.caps-lock-warning').toggle(pass.caps);
        }
      }
    });
  
    $(window).blur(function(e){

        var pass = document.getElementById("password");
        if(typeof(pass.caps) === 'boolean'){
            pass.caps = null;
            $(pass).next('.caps-lock-warning').hide();
        }
        });
    });
yshpjwxd

yshpjwxd1#

您可以尝试以下代码:
超文本:

<div class="form-group">
        <label for="password">Password<span class="star"> *</span></label>
            <div class="input-group">
               <div class="input-group-prepend bg-transparent">
                    <span class="caps-lock-warning" style="color: brown;">caps lock is on.</span>
                    <span><i class="fa fa-lock text-dark"></i>
                    </span>
               </div>
                <input type="password" class="form-control form-control-lg border-left-0" id="password" placeholder="Password" name="password" required autocomplete="off">
                </div>
            </div>

查询:

$(function(){
    $('.caps-lock-warning').hide();
  
    $("#password").keypress(function(e) {
            e = e || window.event;
          var s = String.fromCharCode( e.keyCode || e.which );
          if ( (s.toUpperCase() === s) !== e.shiftKey ) {
             $(this).closest('.input-group').find('.caps-lock-warning').show();
          }else {
          $(this).closest('.input-group').find('.caps-lock-warning').hide();
          }
    });
  });

相关问题