【问题标题】:Trying to add jQuery validation without submit() button尝试在没有 submit() 按钮的情况下添加 jQuery 验证
【发布时间】:2017-10-20 15:44:03
【问题描述】:

下面是我的代码。我想要做的是添加验证,因此当用户单击输入字段时,会显示验证消息“需要名称”。但是,此时它就在输入字段的下方,并且一直存在。谢谢你

$(document).ready(function(){ 
  if($('#firstname').val() == ''){
    $('.errorMsg').show();
  } else{ 
    $('.errorMsg').hide();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" placeholder="First name"     maxlength="15"  <span class="errorMsg">Name required </span>
</input>

【问题讨论】:

  • 如果这是用于实际应用程序,我也建议不要使用 maxlength-attribute。人们往往拥有比大多数 Web 开发人员想象的更有趣的名字
  • @OliverBaumann 但是如果数据库只接受 15,那么它可能会造成一些麻烦。但我同意 15 个字符对于名字来说太少了。
  • @Ivar,如果数据库只接受 15 个 varchars,请与数据库管理员交谈;-) 老实说,这不是一个论点。显然没有冒犯的意思!
  • @OliverBaumann 未采取。只说使用maxlength 本身并不是一件坏事。不管你在数据库中接受多少,总有一个限制。

标签: jquery html validation


【解决方案1】:

使用 CSS 最初隐藏错误消息。您的 HTML 也无效:错误消息 span 不能嵌套在输入中。

工作解决方案:

$(document).ready(function(){ 
  $('#firstname').on('blur', function() {
      if($(this).val() === '') {
        $('.errorMsg').show();
      } else {
        $('.errorMsg').hide();
      }
  });
});
.errorMsg {
    display: none;
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" placeholder="First name" 
 maxlength="15"/>
 <span class="errorMsg">Name required </span>

【讨论】:

    【解决方案2】:

    您可以为此使用blur 事件:

    $(document).ready(function() {
        $('#firstname').on('blur', function() {
            if ($('#firstname').val() == '') {
                $('.errorMsg').show();
            } else {
                $('.errorMsg').hide();
            }
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" id="firstname" placeholder="First name" 
       maxlength="15">  <span class="errorMsg" style="display: none;">Name required </span>
    </input>

    【讨论】:

      【解决方案3】:

      使用blur事件,可以使用jQuery直接从找到的元素调用

      $(document).ready(function(){
      
          $('.errorMsg').hide();
      
          $('#firstname').blur(function(){
          if($('#firstname').val() == ''){
              $('.errorMsg').show();
          }
              else{ 
                  $('.errorMsg').hide();
              }
      
          });
      
      
      });
      

      这是JSFiddle

      【讨论】:

        【解决方案4】:

        jQuery Blur - 将事件处理程序绑定到“模糊”JavaScript 事件,或在元素上触发该事件。

        试试 (JSFiddle)

        var firstNameInput = $('input#firstname'),
            errorMsgEl = $('.errorMsg');
        
        // Bind blur on the input for 'first name'
        firstNameInput.bind('blur', function () {
            // Check if input is blank
            if(firstNameInput.val() == '') {
                // Ensure error isn't already displayed
                if (errorMsgEl.length == 0) $("<div class='errorMsg'>Name required</div>").insertAfter(this);
            } else {
                errorMsgEl.remove();
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2013-03-14
          • 2017-01-12
          • 1970-01-01
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-25
          • 1970-01-01
          相关资源
          最近更新 更多