如何在 jQuery 异步中提交表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9933830/
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-08-27 11:29:06  来源:igfitidea点击:

How do I submit a form in jQuery async?

jqueryajaxforms

提问by Itay Moav -Malimovka

In mootools I would do something like $('form_id').send({success:function(res){....}});What is the parallel syntax in jQuery?

在 mootools 我会做类似$('form_id').send({success:function(res){....}});什么是 jQuery 中的并行语法?

Another words:
How would I put my form data (assume id='bob') into the following code

换句话说:
我如何将我的表单数据(假设 id='bob')放入以下代码中

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

回答by naspinski

This should do it:

这应该这样做:

$.ajax({   
   type: 'POST',   
   url: url,   
   data: $('#bob').serialize(),
   success: success,
   dataType: dataType 
}); 

回答by Matthew Blancarte

Wouldn't you know... It's right there in the documentation! :P

你不知道吗...它就在文档中!:P

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

Edit: okay okay...

编辑:好吧好吧...

$('#too_cool_form').submit(function(e){
  e.preventDefault();
  //do some verification
  $.ajax({
    url: '',
    data: $(this).serialize(),
    success: function(data)
    {
      //callback methods go right here
    }
  });
});

回答by paislee

There is nothing that ships with jQuery that will automatically AJAXify a normal form for you.

jQuery 附带的任何东西都不会自动为您 AJAX 化普通表单。

Option 1-- Intercept the form's submitevent, scrape the data from form fields using serialize, and send using ajaxor post, as suggested.

选项 1- 拦截表单的submit事件,使用 抓取表单字段中的数据serialize,并按照建议使用ajax或发送post

Option 2-- Use this great forms plugin, which does all of option 1 for you.

选项 2- 使用这个很棒的表单插件,它可以为您完成所有选项 1。

回答by Seabass

$.post()or $.ajax()

$.post()或者 $.ajax()

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

The documentation will explain it best: http://api.jquery.com/jQuery.post/

文档将最好地解释它:http: //api.jquery.com/jQuery.post/