简体   繁体   English

提交表单后执行脚本

[英]Executing a script after a form has been submitted

I found, on this site a way to submit a POST form without leaving the page. 我在此站点上发现了一种无需离开页面即可提交POST表单的方法。 In that script, they put instructions on how to have a function take place after the form's been submitted. 在该脚本中,他们指示如何在提交表单后执行功能。 Here's what the script looked like: 脚本如下所示:

$(document).ready(function(){
$form = $('form');
$form.submit(function(){
  $.post($(this).attr('action'), $(this).serialize(), function(response){
  },'json');
  return false;
});
});​

They said, between function(response){ and },'json'); 他们说,在function(response){},'json'); you can specify other JavaScript to take place like alerts etc. after the form's been submitted. 您可以指定提交表单后要执行的其他JavaScript,例如警报等。 I tried 我试过了

$(document).ready(function(){
$form = $('form');
$form.submit(function(){
  $.post($(this).attr('action'), $(this).serialize(), function(response){
$('form').hide();
  },'json');
  return false;
});
});

Unfortunately, that does not work, can anybody tell me why? 不幸的是,那行不通,有人可以告诉我为什么吗? I've set up a jsFiddle . 我已经设置了jsFiddle Please help. 请帮忙。 Thanks. 谢谢。

Using $.post, the "function(response)" is only called AFTER a successful result, so you have been misinformed about doing work within this function while the server is processing the request. 使用$ .post,“ function(response)”仅在成功结果后才被调用,因此,当服务器正在处理请求时,您对在此函数中进行工作的了解不正确。

To continue processing after sending the request to the server, place the $('form').hide() after the $.post call: 要在将请求发送到服务器后继续处理,请将$('form')。hide()放在$ .post调用之后:

$form.submit(function(){
  $.post(
    $(this).attr('action'), $(this).serialize(), 
    function(response){
    }
    ,'json'
  );
  $('form').hide();
  return false;
});

Problems: 问题:

  1. Set JavaScript variable as form , not $form . 将JavaScript变量设置为form ,而不是$form Then use it in jQuery as $(form) . 然后在jQuery中将其用作$(form)
  2. The context of $(this).attr('action') is the $.post() callback, not the form. $(this).attr('action')的上下文是$.post()回调, 而不是表单。 Solution: $(form) instead of $(this) . 解决方案: $(form)而不是$(this)

Solution: 解:

$(document).ready(function(){
    form = $('form');

    $(form).on('submit', function() {
        $.post($(form).attr('action'), $(this).serialize(), function(response) {
            $('form').hide();
        },'json');

        return false;
    });
});

Your fiddle worked fine when the form is changed to 将表格更改为时,您的提琴效果很好

<form action="#" method="post">

And the documentation at http://api.jquery.com/jQuery.post/ shows that this is a valid syntax and http://www.johnnycode.com/blog/2010/04/08/jquery-form-serialize-doesnt-post-submit-and-button-values-duh/ is also a running sample. http://api.jquery.com/jQuery.post/上的文档显示这是有效的语法,并且http://www.johnnycode.com/blog/2010/04/08/jquery-form-serialize- nott-post-submit-and-button-values-duh /也是一个正在运行的示例。 If it's not working at all for you then, as silly as this is, do you have a reference to jQuery in your page? 如果它对您根本不起作用,那么很愚蠢,您是否在页面中引用了jQuery?

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

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