【问题标题】:JQuery not triggering on focusoutJQuery没有触发焦点
【发布时间】:2021-08-15 03:06:36
【问题描述】:

我经营一个商业网站,并试图在客户端阻止某些免费电子邮件地址未在我的网站上注册(服务器端将单独处理)。我看了几篇帖子,在https://stackoverflow.com/a/8277306/857629看到了skinsey的回答。

我必须对代码进行的唯一真正更改是在输入框的focusout 上触发检查,然后显示一条消息并清除输入框。

我对 JQuery 很陌生,无法弄清楚为什么当输入框失去焦点时下面的代码没有触发。

有问题的元素是:

<input type="email" name="email" id="inputEmail" placeholder="your contact email">

我的 JQuery 如下:

jQuery('#inputEmail').focusout(function(){

    // Get the email value from the input with an id="inputEmail"
    var email_addr = jQuery('#inputEmail').val();

    // The regex to check it against
    var re = '[a-zA-Z_\.-]+@((hotmail)|(yahoo)|(gmail)|(outlook)|(protonmail)|(aol))\.[a-z]{2,4}';

    // Check if the email matches
    if(email_addr.match(re)){
        // Email is on the filter list
        // Return false and don't submit the form, or do whatever
        jQuery('#inputEmail').value = "";
        jQuery('#inputEmail').append("<div>To prevent SPAM, free email addresses are no longer accepted on registration. Please enter a valid coorporate email address.</div>");
        
        return false;
    } else {
        // Email ok
        // Allow the form to be submitted
        return true;
    }
});
</script>

我觉得我错过了一些简单的东西。

任何帮助将不胜感激。

【问题讨论】:

    标签: jquery email-address focusout


    【解决方案1】:

    你可以试试这个

    $('#inputEmail').on( "focusout", function(){
    console.log('Focus Removed')
    } );
    

    【讨论】:

      【解决方案2】:
      $(document).on("focusout","#inputEmail",function(){
              alert("Focus Removed");
          });
      

      【讨论】:

        猜你喜欢
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        • 2011-01-12
        • 2011-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多