微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JQuery 不触发焦点

如何解决JQuery 不触发焦点

我经营一个商业网站,并试图在客户端防止某些免费电子邮件地址不在我的网站上注册(服务器端将单独处理)。我查看了一些帖子,并在 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>

我有一种感觉,我错过了一些简单的东西。

任何帮助将不胜感激。

解决方法

你可以试试这个

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。