表单提交后执行脚本 [英] Executing a script after a form has been submitted

查看:122
本文介绍了表单提交后执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,在这个网站上提交 POST 表单而不离开页面的方法。在该脚本中,他们提供了如何在提交表单后进行功能的说明。这是脚本的样子:

I found, on this site a way to submit a POST form without leaving the page. 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;
});
});​

他们说,在函数(响应){},'json')之间; 您可以在表单提交后指定其他JavaScript进行提醒等。我试过

They said, between function(response){ and },'json'); you can specify other JavaScript to take place like alerts etc. after the form's been submitted. I tried

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

不幸的是,这不起作用,有谁可以告诉我为什么?我设置了 jsFiddle 。请帮忙。感谢。

Unfortunately, that does not work, can anybody tell me why? I've set up a jsFiddle. Please help. Thanks.

推荐答案

使用$ .post,function(response)仅在成功结果后被调用,在服务器正在处理请求时在这个函数中做了错误的工作。

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调用后放置$('form')。hide():

To continue processing after sending the request to the server, place the $('form').hide() after the $.post call:

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

这篇关于表单提交后执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆