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

时间:2021-10-26 20:32:06

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
});

4 个解决方案

#1


17  

This should do it:

这应该这样做:

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

#2


7  

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
    }
  });
});

#3


4  

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

jQuery附带的任何内容都不会为您自动AJAX化一个普通的表单。

Option 1 -- Intercept the form's submit event, scrape the data from form fields using serialize, and send using ajax or post, as suggested.

选项1 - 截取表单的提交事件,使用序列化从表单字段中删除数据,并按照建议使用ajax或post发送。

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

选项2 - 使用这个伟大的表单插件,它为您完成所有选项1。

#4


0  

$.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/

#1


17  

This should do it:

这应该这样做:

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

#2


7  

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
    }
  });
});

#3


4  

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

jQuery附带的任何内容都不会为您自动AJAX化一个普通的表单。

Option 1 -- Intercept the form's submit event, scrape the data from form fields using serialize, and send using ajax or post, as suggested.

选项1 - 截取表单的提交事件,使用序列化从表单字段中删除数据,并按照建议使用ajax或post发送。

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

选项2 - 使用这个伟大的表单插件,它为您完成所有选项1。

#4


0  

$.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/