【问题标题】:How to add the jQuery validation in textbox focusout event?如何在文本框焦点事件中添加 jQuery 验证?
【发布时间】:2016-09-01 12:46:12
【问题描述】:

如何仅在 focusout 事件中向文本框控件添加验证?

有人有想法吗?

【问题讨论】:

  • 显示您当前正在进行验证的代码
  • 你的代码在哪里?你尝试了什么?

标签: jquery jquery-focusout


【解决方案1】:

你的 html 是:

<input type="text">

还有你的 jQuery:

$('input').blur(function(){
    var text = $(this).val();
   //do smth with your text...
});

【讨论】:

    【解决方案2】:

    $("#txt1").blur(function(){
      var v = $("#txt1").val();
      if(v == "test"){
        alert('valid');
      }
      else{
        alert('invalid');
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    T1: <input type="text" id="txt1">
    T2: <input type="text" id="txt2">

    参考:https://api.jquery.com/focusout/

    .focusout(function() {
    
        // Put your validation code here
        if(!valid){
            return false;
        }    
    })
    

    .blur(function() {
    
        // Put your validation code here
        if(!valid){
            return false;
        }    
    
    })
    

    【讨论】:

    • 如果尝试这种方式,keyup 事件也会发生验证
    • 请尝试使用代码 sn-p。是不是你想要的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2017-09-20
    • 2012-04-01
    • 2010-11-06
    • 2013-11-24
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多