简体   繁体   English

alert()触发表单提交

[英]alert() triggers form submission

I am trying to eliminate the "enter event" default value on type=text , which triggers submit on the form. 我试图消除 type=text的“ enter event”默认值 ,这会触发在表单上提交。 I do so with the following: 我这样做如下:

var onEnter = function(values){
    $("#"+values['id']).keydown(function(e){
        if (e.which == 13) {
            event.preventDefault();
            var function_to_call = window[values['function']];
            if(typeof function_to_call === 'function'){
                if(values['parameters']!= null){
                    parameters = values['parameters'];
                    var args;

                    if(typeof parameters === 'string')
                        args = parameters.split(',');
                    else
                        args = new Array(parameters);

                    function_to_call.apply(window, args);
                }
                else function_to_call();
            }
            return false;
        }
    });
}

The code that matters for taking away the default event is 带走默认事件重要的代码是

event.preventDefault();
return false;

The rest of code just calls a function with parameters or no parameters. 其余代码只调用带有参数或不带参数的函数。 The thing is, inside the functions called, there´sa condition that triggers an alert. 问题是在调用的函数内部,有一个条件会触发警报。 Here´s the deal: 这是交易:

  1. If alert is not triggered , [enter] does not trigger form submission 如果未触发警报 ,则[enter]不会触发表单提交
  2. if alert is triggered , form submission is triggered 如果触发警报,则触发表单提交

The function called on every type=text is the same, and here is the alert condition: 在每个type=text上调用的函数都是相同的,这是警报条件:

......
if(!regex_match(item)){
        alert("Solo son posibles los siguientes caracteres: '.' ',' 'a-z' 'a-Z' '0-9' y ' '");
        return;
 }
 .....

var regex_match = function(text){
    regex = /^[_., 0-9a-záéíóúñ]+$/i;
    return regex.test(text);
}

Things tried: 尝试过的事情:

  • " return false; " after alert(...) ; return false;alert(...)

I am new to javascript, could anyone point out why this could be happening? 我是javascript新手,有人能指出为什么会这样吗? Thanks in advance! 提前致谢!

I'm fairly surprised by that (and expect you'll find the behavior varies from browser to browser), but alert is a funny beast somewhat at odds with the rest of the browser environment, so... 我对此感到非常惊讶(并希望您会发现行为因浏览器而异),但是alert是一个有趣的野兽,与其他浏览器环境有些矛盾,所以...

Since you always want to stop the default behavior, I'd recommend either: 由于您总是想停止默认行为,因此我建议:

  1. Don't use alert , which is fairly ugly anyway (use any of the many "modal" dialog plug-ins for jQuery, or just do your own with a styled, absolutely-positioned div ), or 无论如何都不要使用alert ,这非常丑陋(使用jQuery的许多“模式”对话框插件中的任何一个,或者只使用样式固定且绝对定位的div自己做),或者

  2. Take the alert out of the event handling sequence, like this: 从事件处理序列中删除alert ,如下所示:

     var onEnter = function (values) { $("#" + values['id']).keydown(function (e) { if (e.which == 13) { setTimeout(finishHandling, 0); return false; } function finishHandling() { var function_to_call = window[values['function']]; if (typeof function_to_call === 'function') { if (values['parameters'] != null) { parameters = values['parameters']; var args; if (typeof parameters === 'string') args = parameters.split(','); else args = new Array(parameters); function_to_call.apply(window, args); } else function_to_call(); } } }); } 

    What this second option does is let the event handler complete before you call alert , by scheduling a callback to finishHandling to occur almost immediately, but after the event handling has been completed. 第二个选项的作用是让事件处理程序在您调用alert之前完成,方法是调度对finishHandling的回调几乎立即发生,但要在事件处理完成之后进行。 (I also removed the call to event.preventDefault() , because return false; from a jQuery event handler does both event.preventDefault() and event.stopPropagation() . You may have needed it when the alert was there, but you don't without it.) (我还删除了对event.preventDefault()的调用,因为return false;从jQuery事件处理程序中return false; event.preventDefault()event.stopPropagation() 。当alert在那里时,您可能需要它,但您没有不是没有。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM