简体   繁体   English

提交表格并留在同一页面上?

[英]Submit form and stay on same page?

I have a form that looks like this 我有一个看起来像这样的表格

<form action="receiver.pl" method="post">
  <input name="signed" type="checkbox">
  <input value="Save" type="submit">
</form>

and I would like to stay on the same page, when Submit is clicked, but still have receiver.pl executed. 当我点击提交时,我想留在同一页面,但仍然执行了receiver.pl

How should that be done? 应该怎么做?

The easiest answer: jQuery. 最简单的答案:jQuery。 Do something like this: 做这样的事情:

$(document).ready(function(){
   var $form = $('form');
   $form.submit(function(){
      $.post($(this).attr('action'), $(this).serialize(), function(response){
            // do something here on success
      },'json');
      return false;
   });
});

If you want to add content dynamically and still need it to work, and also with more than one form, you can do this: 如果要动态添加内容并仍需要它可以使用,并且还需要多个表单,则可以执行以下操作:

   $('form').live('submit', function(){
      $.post($(this).attr('action'), $(this).serialize(), function(response){
            // do something here on success
      },'json');
      return false;
   });

99% of the time I would use XMLHttpRequest or fetch for something like this. 99%的时间我会使用XMLHttpRequestfetch来做这样的事情。 However, there's an alternative solution which doesn't require javascript... 但是,有一个替代解决方案,不需要javascript ...

You could include a hidden iframe on your page and set the target attribute of your form to point to that iframe. 您可以在页面上包含隐藏的iframe,并将表单的target属性设置为指向该iframe。

<style>
  .hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>

<iframe name="hiddenFrame" class="hide"></iframe>

<form action="receiver.pl" method="post" target="hiddenFrame">
  <input name="signed" type="checkbox">
  <input value="Save" type="submit">
</form>

There are very few scenarios where I would choose this route. 我很少会选择这条路线。 Generally handling it with javascript is better because, with javascript you can... 一般用javascript处理它更好,因为使用javascript你可以......

  • gracefully handle errors (eg retry) 优雅地处理错误(例如重试)
  • provide UI indicators (eg loading, processing, success, failure) 提供UI指标(例如加载,处理,成功,失败)
  • run logic before the request is sent, or run logic after the response is received. 在发送请求之前运行逻辑,或在收到响应后运行逻辑。

HTTP / CGI方法是让程序返回HTTP状态代码204(无内容)。

When you hit on the submit button, the page is sent to the server. 当您点击提交按钮时,页面将被发送到服务器。 If you want to send it async, you can do it with ajax. 如果要将其发送为异步,可以使用ajax进行。

Use XMLHttpRequest 使用XMLHttpRequest

var xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);

//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function() { // Call a function when the state changes.
    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
        // Request finished. Do processing here.
    }
}
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array()); 
// xhr.send(document);

Just use: action="" . 只需使用: action="" There's no need for anything like javascript. 没有像javascript这样的东西。

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

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