我想打印用户以 html 形式提交的数据

I want to print the data the user has submitted in html form

我想在用户点击“提交您的条目”按钮后打印他提交的数据

并为他显示按摩说: 感谢“名字”的评论.. 您的电子邮箱:example@yahoo.com 您在我的网站上喜欢的东西:网站设计和内容。例如

这是我的代码:

<html>
  <head>
    <title>Feedback form</title>
<form method = "post" action = "">
    <title>Feedback form </title>

  </head>
  <body>

  <h1>Feedack Form</h1>
<p>Please fill out this form to help me improve My site.</p>

<p><lable>Name:
  <input name="name" type="text" size="20"/> <br>

  E-mail Addres:
    <input name="email" type="email" size="20"/>
  </lable>

  <p>
    <strong>Things you like on my site:</strong> <br>

    <label>Site design
<input name = "thingsliked" type = "checkbox" value = "Design" /></label>
<br>
<label>Contents
<input name = "thingsliked" type = "checkbox" value = "Contents" /></label>
<br>
 <label>Ease of use
<input name = "thingsliked" type = "checkbox" value = "Ease" /></label>
<br>


<input type = "submit"  value = "Submit Your Entries"/>
 <input type = "reset" value = "Clear Your Entries" />
  </body>
</html>

看看这个

  1. 您需要在提交之前包裹一个表单 and/or 重置 - 您的表单标签不能位于页面的头部
  2. 我防止这里默认不提交,你必须想办法提交,否则用户看不到结果——例如另一个提交按钮或使用 AJAX

document.getElementById("feedback").addEventListener("submit", function(e) {
  e.preventDefault(); // stop submission
  const html = [...this.elements]
    .filter(ele => {
      if (ele.type === "text" || ele.type==="email") return true;
      if (ele.type === "checkbox" && ele.checked) return true;
      if (ele.type === "submit" || ele.type === "reset") return false;
    })
    .map(ele => {
      const name = ele.name
      const value = ele.value;
      if (ele.classList.contains("thingslike")) return `<tr class="thingslike"><td>${ele.name}</td><td>&nbsp;</td></tr>`
      else return `<tr><td>${ele.name}</td><td>${ele.value}</td></tr>`
    }).join("");
  document.getElementById("res").innerHTML = `You answered: <br/><table id="answer">
   <tbody>${html}</tbody>
   </table>`;
   const like = document.querySelector("#answer .thingslike");
   if (like) like.insertAdjacentHTML("beforebegin",`<tr><td colspan="2">Things you like:</td></tr>`)
})
<h1>Feedback Form</h1>
<p>Please fill out this form to help me improve My site.</p>
<form id="feedback">
  <p>
    <label>Name:
    <input name="name" type="text" size="20" /> <br> E-mail Addres:
    <input name="email" type="email" size="20" />
  </label>

    <p>
      <strong>Things you like on my site:</strong> <br>

      <label>Site design
<input name="Design" class="thingslike" type="checkbox" /></label>
      <br>
      <label>Contents
<input name="Contents" class="thingslike" type="checkbox" /></label>
      <br>
      <label>Ease of use
<input name="Ease of use" class="thingslike" type="checkbox" /></label>
      <br>
      <input type="submit" value="Submit Your Entries" />
      <input type="reset" value="Clear Your Entries" />
</form>

<div id="res"></div>