简体   繁体   English

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

[英]submit button from hidden form refresh the page

I have a form , after validate I take data and update the hidden form that I popup or just need to be hidden and need to be posted to the action. 我有一个表单,经过验证后,我将获取数据并更新弹出的隐藏表单,或者只是需要隐藏并需要发布到操作中。

from hidden I do manual submit from code the bug is that always post by submit lead to refresh the page instead. 从隐藏中我确实从代码中手动提交了一个错误,那就是总是通过提交线索来刷新页面。 redirecting 重新导向

my js code:(could be typo) . 我的js代码:(可能是拼写错误)。 - form validate -表格验证

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.


    }

my hidden form (even if it is not hidden click on submit will casue refresh) 我的隐藏表单(即使不是隐藏表单,请单击提交也会继续刷新)

          <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>

It's working without slash at the end in the url on action But the post data is not send in that case 实际运作中的网址末尾没有斜杠,但是在这种情况下不会发送发布数据

It happens because the form IS submitted. 这是因为表单已提交。 You need to return false in your submithandler, like this: 您需要在submithandler中返回false,如下所示:

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

in submit handler function, use 在提交处理程序函数中,使用

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

where e being the event passed. e是事件通过的地方。

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

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