如何在jquery或javascript中创建表单数据对象?

如何在jquery或javascript中创建表单数据对象?,javascript,jquery,ajax,forms,Javascript,Jquery,Ajax,Forms,我想用AJAX提交两个不同的表单,但我无法确定如何选择这些表单来创建表单数据对象(它也有文件) ).我试过一种形式: <form class="ui form" id="contact-form" enctype="multipart/form-data" onsubmit='return !!(filecheck() && show());'> <div class="field"> <label>Title of the Ad&

我想用AJAX提交两个不同的表单,但我无法确定如何选择这些表单来创建表单数据对象(它也有文件) ).我试过一种形式:

<form class="ui form" id="contact-form" enctype="multipart/form-data" onsubmit='return !!(filecheck() && show());'>
  <div class="field">
    <label>Title of the Ad</label>
    <input name="title" type="text" id="title">
  </div>
  <div class="field">
    <label>Select Car Make</label>
</form>
现在我有另一张表格

<form class="ui form" id="second-form" enctype="multipart/form-data" onsubmit='return !!(filecheck() && show());'>
  <div class="field">
    <label>Title of the Ad</label>
    <input name="title" type="text" id="title">
  </div>
  <div class="field">
    <label>Select Car Make</label>
</form>

广告名称
选择汽车品牌

如何发送此表单的请求…有什么想法吗?

您可以通过使用两个表单上的公共类来选择它们来实现这一点。然后,您可以在
submit
事件处理程序中使用
this
关键字,仅引用触发事件的表单:

$('.ui.form').submit(function(e) {
  e.preventDefault();
  var formData = new FormData(this); // note use of 'this' here

  $.ajax({
    url: 'infocheck.php',
    cache: false,
    data: formData,
    contentType: false, 
    processData: false,
    type: 'post',
    success: function(response) {    
      document.getElementById("loader").style.display = "none";
      alert(response);
    }
  });
});

对于$(this)[0]和$(this)[1]这两种形式,可能存在重复的操作方法@RoryMcCrossan@gANDALF你是说你想在一个请求中同时发送两个表单吗?你能告诉我如何在jquery中按id或类选择表单,就像我们为元素var element=document做一些事情一样,我有点困惑您的原始代码是通过
id
选择的,而我上面的示例是通过
class
选择的,所以我对您的要求有点困惑。我有两个单独的表单请求,两个不同的ajax块,并被传递到两个不同的URL,我只是在创建这些表单数据对象时感到困惑。对不起,我仍然不知道你在问什么或者问题是什么。如果您想发出两个单独的请求,只需复制原来的代码并更改选择器。