隐藏表单中的“提交”按钮刷新页面

时间:2018-08-16 19:11:52

标签: javascript jquery css

我有一个表单,经过验证后,我将获取数据并更新弹出的隐藏表单,或者只需要将其隐藏并需要发布到操作中即可。

从隐藏中我手动从代码提交 错误是总是通过提交导致发布来刷新页面。重定向

我的js代码:(可能是拼写错误)。 -表单验证

form.validate({
    ignoreTitle: true,
    onfocusout: function (element) {
        if (!this.checkable(element)) {
            this.element(element);
        }
    },
    onkeyup: false,
    rules: {
        firstName: {required: true, minlength: 2, maxlength: 45},
        lastName: {required: function () {
            return checkField("lastName")
        }, minlength: 2, maxlength: 45},

    },
    messages: {
        firstName: {required: FieldRequiredStr, minlength: invalidFirstName, maxlength: invalidFirstName},
        lastName: {required: FieldRequiredStr, minlength: invalidLastName, maxlength: invalidLastName},

    },
    onsubmit: true,
    submitHandler: function (frm) {
        if(!form.valid())return;

        $("#send",form).attr('disabled', 'disabled');
        $("#send",form).addClass('sent');

        form.ajaxSubmit(
            {
                url: "api.php",
                dataType: 'xml',
                success: function (response) {
                    var xml;

                    if (typeof response == 'string') {
                        xml = new ActiveXObject("Microsoft.XMLDOM");
                        xml.async = false;
                        xml.loadXML(response);
                    }


                        var url = "https://url/login"
                        console.log(url);
                        $('#frm2').attr('action', url );
                        $('#email').val($("#email",frm).val());
                        $('#password').val($("#password",frm).val());
                       // $.fancybox({href: '#make-deposit'});
                        //$("#frm2").submit();
                        //
                        setTimeout(function () {
                            $("#frm2").submit();

                        }, 2000);


                    }
                }
            });

    },
    errorPlacement: function (error, element) {

            //this is working good
        // A bit ugly.


    }

我的隐藏表单(即使不是隐藏表单,请单击提交也会刷新)

          <div style="display:none;">

                    <form id="frm2" method="post" action="https://url/login" target="">
        <input type="hidden" id="email" name="email" value="" />
         <input type="hidden" id="password" name="password" value="" /> 
           <input class="dbtn" type="submit" name="btn" id="btn" value="btn" />

                    </form>
            </div>

正在运行的网址末尾没有斜杠  但是在这种情况下,帖子数据不会发送

2 个答案:

答案 0 :(得分:1)

发生这种情况是因为表格已提交。您需要在submithandler中返回false,如下所示:

submitHandler: function (frm) {
 //Your code here
return false;
}

答案 1 :(得分:1)

在提交处理程序函数中,使用

e.preventDefault();
e.stopPropogation();

传递事件的地方。