Javascript 如何从表单收集数据并通过ajax POST发送(从jQuery到Vanilla JS)

Javascript 如何从表单收集数据并通过ajax POST发送(从jQuery到Vanilla JS),javascript,jquery,html,Javascript,Jquery,Html,我在jQuery中有这个联系人表单函数writien,但我正在用VanlliaJS实现我的站点 以下是表单的HTML: <form id="contact-form" method="post"> <label for="required_question" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;"></label&

我在jQuery中有这个联系人表单函数writien,但我正在用VanlliaJS实现我的站点

以下是表单的HTML:

<form id="contact-form" method="post">
  <label for="required_question" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;"></label>
  <label for="required_question2" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;"></label>
  <label for="name" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;">name</label>
  <label for="email" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;">email</label>
  <label for="subject" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;">email</label>
  <label for="text" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;">message</label>
  <label for="submit" style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;">submit</label>

  <input tabindex="-1" name="required_question" placeholder="Required..." style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;"></input>
  <input tabindex="-1" name="required_question2" placeholder="Required..." style="position:absolute; left:-10000px; bottom:auto; width:1px; height:1px; overflow:hidden;"></input>
  <input type="text" name="name" placeholder="Name..." required></input>
  <input type="email" name="email" placeholder="Email..." required></input>
  <input type="text" name="subject" placeholder="Subject..." required></input>
  <textarea name="text" placeholder="Enter your message here" required></textarea>
  <input type="submit" class="submit" value="Submit."></input>
</form>
以下是我到目前为止在JS中的内容:

document.getElementById('contact-form').onsubmit = function(){
  document.querySelector('input[type="submit"]').value = 'Sending...';
  document.querySelector('input[type="submit"]').blur();

  var $data = {};
  var $contact_data = this.querySelectorAll('input[name], textarea[name]');

  for(i = 0; i < $contact_data.length; i++){
    $data[this.getAttribute('name')] = this.value;
  }
  Array.prototype.forEach.call($contact_data, function(index, value){
    console.log($data)
  });
  return false;
}
document.getElementById('contact-form')。onsubmit=function(){
document.querySelector('input[type=“submit”]”)。值='Sending…';
document.querySelector('input[type=“submit”]).blur();
var$data={};
var$contact_data=this.querySelectorAll('input[name],textarea[name]);
对于(i=0;i<$contact_data.length;i++){
$data[this.getAttribute('name')]=this.value;
}
Array.prototype.forEach.call($contact\u数据,函数(索引,值){
console.log($data)
});
返回false;
}

它似乎记录表单数据,但返回值为“null:undefined”;请帮忙

我相信您的问题在于单词
this
this.getAttribute
this.value
将引用表单,而不是单个输入的DOM

[编辑]我建议使用
forEach
方法,该方法是
querySelectorAll
返回的节点列表的一部分,如下所示:

var $data = {};
var $contact_data = this.querySelectorAll('input[name], textarea[name]');
$contact_data.forEach(function($input) {
  $data[$input.getAttribute('name')] = $input.value;
});
这样做可以消除对迭代器的需求,而且更干净。如果您不需要
$contact\u data
变量,也不需要该变量,您可以使用:

var $data = {};
his.querySelectorAll('input[name], textarea[name]').forEach(function($input) {
  $data[$input.getAttribute('name')] = $input.value;
});

我希望这有帮助。

我相信您的问题在于单词
this
this.getAttribute
this.value
将引用表单,而不是单个输入的DOM

[编辑]我建议使用
forEach
方法,该方法是
querySelectorAll
返回的节点列表的一部分,如下所示:

var $data = {};
var $contact_data = this.querySelectorAll('input[name], textarea[name]');
$contact_data.forEach(function($input) {
  $data[$input.getAttribute('name')] = $input.value;
});
这样做可以消除对迭代器的需求,而且更干净。如果您不需要
$contact\u data
变量,也不需要该变量,您可以使用:

var $data = {};
his.querySelectorAll('input[name], textarea[name]').forEach(function($input) {
  $data[$input.getAttribute('name')] = $input.value;
});

我希望这有帮助。

您引用表单元素而不是输入。您应该更改:

$data[this.getAttribute('name')] = this.value;


您可以引用表单元素而不是输入。您应该更改:

$data[this.getAttribute('name')] = this.value;


您能为我们提供包含给定ID的HTML元素吗?谢天谢地,这件事把它添加到了问题中。谢谢@rojon其中一个HTML元素显示了任何id标记的使用…@Rojo检查表单元素能否向我们提供包含给定id的HTML元素?谢天谢地,这件事把它添加到了问题中。谢谢@rojon您的一个HTML元素显示了任何id标记的使用…@Rojo check form element我尝试将其更改为$contact_data,但它破坏了我的代码,我看不到错误,因为返回false未运行我缺少循环通过元素的[i](我想它代表整数)。新手错误我知道,但我几乎没有从jQuery移动到javascriptI我在手机上,所以很难编写代码,但我在回答中添加了。我尝试将其更改为$contact_data,但它破坏了我的代码,我看不到错误,因为返回false没有运行我缺少[I](我认为它代表整数)使循环在元素之间循环。菜鸟的错误我知道,但我几乎没有从jQuery移动到javascriptI我在我的手机上,所以写代码很困难,但我补充了我的答案;现在我只需要通过post将这些数据发送到我的php函数Thank you@MuffinWithJam,我不需要数组循环;现在我只需要通过post将这些数据发送到我的php函数