javascript - 空格键解除警报

标签 javascript jquery html alert

我有一个技术问题,如果在输入框中输入空格,它会提示“不允许有空格”,但是由于空格键也可用于关闭提示,因此它会在按键时立即消失。所以警报只是闪烁然后就消失了,除非你按住空格键。谁能弄清楚如何解决这个问题?谢谢。

$('#email').bind('DOMAttrModified textInput input change keypress paste',function(){  

   if($(this).val().match(/[\s]/g)) {
     alert("email should not contain spaces");
   } 
});   

最佳答案

您可以创建自己的模态,而不是使用 JavaScript 模态。您可以使用 jQuery UI 对话框,但这并不难做到。

此外,您绑定(bind)到所有这些不同类型的事件这一事实可能会导致其中一些事件不恰本地触发多次。您可以检查您是否已经在使用简单的数据标志警告用户。

$('#email').bind('DOMAttrModified textInput input change keypress paste', function (e) {
    var $this = $(this);
    if ($this.val().match(/[\s]/g) && !$this.data('spacedout')) {
        $this.data('spacedout', true);
        $("<div>").appendTo("body").css({
            position: 'fixed',
            width: '100%',
            height: '100%',
            backgroundColor: 'black',
            opacity: '0.7',
            top: 0,
            left: 0
        }).append($("<div style='color: red;'>Spaces not allowed<br><input type=submit value=OK></div>").on('click', function () {
            $this.data('spacedout', false).trigger('focus');
            $(this).parent().remove();
        }));
        $this.trigger('blur');
    }
});

http://jsfiddle.net/ExplosionPIlls/HwYdF/

关于javascript - 空格键解除警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15575241/

相关文章:

c# - 结合 C# 和 javascript 代码

javascript - 手机网站如何锁定自动旋转?

javascript - 如何附加图像序列化?

javascript - Razor 和 Ajax : How to reload a table upon successful Ajax call?

javascript - Jquery UI 从 slider 获取值

c# - 如何将多个进度 div 添加到基于 List<T> 对象项的 div

javascript - 嵌入 3D 模型查看器

javascript - 优化 jQuery 回调

具有水平和垂直滚动以及固定标题和固定列宽的 html 表格

javascript - pac(proxy-auto-config)文件中的FindProxyForURL函数在IE浏览器中无法工作