简体   繁体   English

将Submit事件附加到我的表单时,使用默认处理程序提交表单

[英]when attaching submit event to my form, the form is submitted with default handler

This happens in IE and Firefox. 这发生在IE和Firefox中。 Not in chrome. 不在镀铬中。

I have this form: 我有这个表格:

        <form id="enviaEmail" method="post">
            <label for="nombre">Nombre</label>
            <input id="nombre" type="text" name="nombre" required="" />

            <label for="apellido">Apellido</label>
            <input id="apellido" type="text" name="apellido" required="" />

            <label for="email">E-mail</label>
            <input id="email" type="email" name="email"  required="" />

            <label for="telefono">Teléfono</label>
            <input id="telefono" type="text" name="telefono" />

            <label for="mensaje">Comentarios</label>
            <textarea id="mensaje" name="mensaje" required="" ></textarea>

            <input id="submit" type="submit" name="submit" value="Enviar" />
        </form> 

And this javascrpt to attach my own event that does a post by mean of Ajax: 这个javascrpt附加我自己的事件,该事件通过Ajax进行发布:

(function ($) {
    $(document).ready(function () {
        // ação que oculta a mensagem ao clicar no x
        $(".close").on('click', function () {
            $(this).parent().fadeOut();
        })
        // REQUISIÇÃO AJAX ENVIO DE E-MAIL
        $("#enviaEmail").on("submit", function (event) {
            event.preventDefault();

            // pega todos inputs
            var value = $(this).serializeArray(),
                    // objeto de dados para requisição
                    request = {
                        'option': 'com_ajax',
                        'module': 'wgajaxcontato',
                        'data': value,
                        'format': 'jsonp'
                    };

            // requisição ajax
            $.ajax({
                type: 'POST',
                data: request,
                beforeSend: function () {
                    $('.hum_form_contact .formulario .loading').fadeIn().css('display', 'table');
                    $("#enviaEmail #submit").attr("disabled", true);
                },
                success: function (resposta) {
                    $("#resposta").fadeIn().html(resposta).delay(5000);
                    //$("#resposta").fadeIn().html(resposta).delay(5000).fadeOut(500);
                },
                complete: function () {
                    $(".hum_form_contact .formulario .loading").fadeOut().css('display', 'none');
                    $("#enviaEmail #submit").attr("disabled", false);
                    $("#nombre").val("");
                    $("#apellido").val("");
                    $("#email").val("");
                    $("#assunto").val("");
                    $("#telefono").val("");
                    $("#mensaje").val("");
                    $("#sccaptcha").val("");
                }
            });
        });
    });
})(jQuery);

The problem is that in chrome, all works perfect. 问题是在chrome中,所有功能都完美无缺。 That is, I fill the form and then when I press "submit" button, the form sends the post using the code in my custom event handler. 也就是说,我填写了表单,然后当我按下“提交”按钮时,表单使用我的自定义事件处理程序中的代码发送了帖子。

In Firefox or IE, when I press submit button, the form is submitted using the default handler, so the whole page is reloaded. 在Firefox或IE中,当我按下“提交”按钮时,将使用默认处理程序提交表单,因此将重新加载整个页面。 I can realize about this because if I place an alert inside my handler, it is not shown. 我可以意识到这一点,因为如果我将警报放置在处理程序中,则不会显示该警报。

What is wrong here? 怎么了

No errors are shown in console, so it seems there is no an error in the HTML or in Javascript. 控制台中未显示任何错误,因此HTML或Javascript中似乎没有错误。

Thanks Jaime 谢谢海梅

Old IE versions had event.returnValue = false Try this to support IE and other browser: event.preventDefault ? event.preventDefault() : event.returnValue = false; IE的旧版本具有event.returnValue = false尝试此操作以支持IE和其他浏览器: event.preventDefault ? event.preventDefault() : event.returnValue = false; event.preventDefault ? event.preventDefault() : event.returnValue = false;

IE Legacy event properties IE旧版事件属性

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

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