javascript 提交表单而不重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21115104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:06:40  来源:igfitidea点击:

submit form without redirecting

javascripthtmlajaxredirect

提问by user3182036

I want to submit a form to action page which is a different site however i don't want to be directed to the action page. i've tried everything but its not working for me, i've heard about ajaxdoes the job can someone give me examples

我想将表单提交到不同站点的操作页面,但是我不想被定向到操作页面。我已经尝试了一切,但它对我不起作用,我听说过ajax这项工作有人能给我举个例子吗

What I want:

我想要的是:

  • i want to submit a form but not be redirected
  • submit the form as i'm being redirected to action page i get redirected to my original page.
  • 我想提交表单但不被重定向
  • 当我被重定向到操作页面时提交表单我被重定向到我的原始页面。

My code:

我的代码:

<form id="user-login-form" accept-charset="UTF-8" action="http://xxxxxn"  method="post">
<div>
<div class="user_login_block&amp;op=Log+in"><label for="edit-name"><span class="form-required" title="This field is required.">*</span></label>
<input type="hidden" />
<div>
<div class="user_login_block&amp;op=Log+in"><label for="edit-name"><span class="form-required" title="This field is required.">*</span></label>
<input type="hidden" />
<input class="form-text required" id="edit-name" type="hidden" maxlength="60" name="name" size="15" value="xxxxxx2" /></div>
<div class="form-item form-type-password form-item-pass"><label for="edit-pass"></label>
<input class="form-text required" id="edit-pass" type="hidden" maxlength="60" name="pass" size="15" value="9sQ&amp;ampzW#7PKhpqbzcCFz9jvY" />
<input type="hidden" /></div>
</div>
<input type="hidden" name="form_build_id" value="form-odWc3MFMsKIL_5vCQtPmiv0AVf0tFwBu7eP2-8" />
<input type="hidden" name="form_id" value="user_login_block" />
<div class="form-actions form-wrapper" id="edit-actions"><input class="form-submit" id="edit-submit" type="submit" name="op" value="submit" />

回答by Susheel Singh

Check the below example:

检查以下示例:

check this link: http://susheel61.0fees.net/ajaxtest.php

检查此链接:http: //susheel61.0fees.net/ajaxtest.php

NOTE:This is basic a example for ajax. You cant do it many other ways like using $.post, $.get etc. but $.ajax has many options like showing a loading image before the content loads.

注意:这是 ajax 的基本示例。你不能用很多其他的方式来做,比如使用 $.post、$.get 等,但 $.ajax 有很多选项,比如在内容加载之前显示加载图像。

html:

html:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>ajax example</title>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
        <div id="message"></div>
        <form id="myform" action="test.php">
            <input type="text" name="test"/>
            <input type="submit" value="submit"/>
        </form>
        <div id="response"></div>

        <script>
            $(function() {
                $("#myform").on("submit", function(e) {
                    e.preventDefault();
                    $.ajax({
                        url: $(this).attr("action"),
                        type: 'POST',
                        data: $(this).serialize(),
                        beforeSend: function() {
                            $("#message").html("sending...");
                        },
                        success: function(data) {
                            $("#message").hide();
                            $("#response").html(data);
                        }
                    });
                });
            });
        </script>
    </body>
</html>

回答by ohjeeez

If nothing works, you could redirect to a hidden iframe.

如果没有任何效果,您可以重定向到隐藏的 iframe。

<iframe id="iframe" name="my_iframe"></iframe>
<form class="form" id="userform" target="my_iframe">

回答by user6062340

In the php, you can redirect to back to the form by using headers:

在 php 中,您可以使用标题重定向回表单:

header('Location: yourwebsite.com');

Hopefully this works for you

希望这对你有用

回答by Prakash R

try this:

试试这个:

$('form').submit(function(event){
var serialObject = $(this).serialize();
//ex for post request
   $.post('path_to file',{
     parameters:serialObject
   },function(data){
         // do something after submitting
     });
   });

event.preventDefault()