如何控制台记录整个 HTML 表单?

How to Console log the Entire HTML Form?

我正在尝试通过控制台记录整个表单,但 JavaScript 代码变得太长了。任何人都可以帮助我如何遵循 DRY(不要重复自己),因为我在我的脚本标签中重复了很多代码

<form class="form" action="register.jsp" method="post">
<ul class="fieldlist">
    <li>
<label for="simple-input"> UserName < /label> < input id="simple-input1" name="userid" type="text" class="k-textbox" style="width: 100%;" />
 </li>
 <li>
 <label for="simple-input">Password</label>
 <input id="simple-input2" name="pwd" type="password" class="k-textbox" style="width: 100%;" />
        </li>
<li>
<input onclick="myFunction()" id="button" type="submit">
</li> </ul>
</form>

这是我的脚本标签。我想遵循 DRY 规则。我尝试将值保存到每个单独的变量中。

    <script>
    var nameInput = document.getElementById('simple-input1');

document.querySelector('form.form').addEventListener('submit', function(e) {

    //prevent the normal submission of the form
    e.preventDefault();

    console.log("Username: " + nameInput.value);
});

 var nameInput1 = document.getElementById('simple-input2');

  document.querySelector('form.form').addEventListener('submit', function (e) {

//prevent the normal submission of the form
e.preventDefault();

console.log("Password: " + nameInput1.value);    
});


</script>

您的两个听众都在听同一件事 - 提交。通过将它们合并到一个简化的侦听器中,使用模板文字和换行符 console.log():

使您的代码更简单
document.querySelector("form.form").submit = function(e) {
    e.preventDefault();
    console.log(`Username: ${nameInput.value}\nPassword: ${nameInput1.value}`);
}

您可以使用 FormData and pass the form element as a parameter. Then use FormData.entries() 获取所有值的迭代器

document.querySelector('form.form').addEventListener('submit', function(e) {
  e.preventDefault();
  const formData = new FormData(this);
  const entires = formData.entries();

  for (var input of entires) {
    console.log(input[0] + ': ' + input[1]);
  }
});
<form class="form" action="register.jsp" method="post">
  <ul class="fieldlist">
    <li>
      <label for="simple-input"> UserName </label> <input id="simple-input1" name="userid" type="text" class="k-textbox" style="width: 100%;" />
    </li>
    <li>
      <label for="simple-input">Password</label>
      <input id="simple-input2" name="pwd" type="password" class="k-textbox" style="width: 100%;" />
    </li>
    <li>
      <input id="button" type="submit">
    </li>
  </ul>
</form>

您可以使用 document.querySelector('form.form').elements

获取表单元素

document.querySelector('form.form').addEventListener('submit', function(e) {
  e.preventDefault();
  let x = document.querySelector('form.form').elements;
  console.log("Username: ", x['userid'].value);
  console.log("Password: ", x['pwd'].value);
});
<form class="form" action="register.jsp" method="post">
  <ul class="fieldlist">
    <li>
      <label for="simple-input"> UserName</label>
      <input id="simple-input1" name="userid" type="text" class="k-textbox" style="width: 100%;">
    </li>
    <li>
      <label for="simple-input">Password</label>
      <input id="simple-input2" name="pwd" type="password" class="k-textbox" style="width: 100%;">
    </li>
    <li>
      <input id="button" type="submit">
    </li>
  </ul>
</form>