首页 > Web开发 > 详细

[HTML 5] FormData

时间:2020-06-18 20:41:04      阅读:56      评论:0      收藏:0      [点我收藏+]
const app = document.getElementById(‘app‘);
app.innerHTML = `
  <h1>JavaScript DOM</h1>
  <form name="order">
    <label>
      Your name
      <input type="text" name="fullname">
    </label>
    <label>
      Which pizza would you like?
      <select name="pizza">
        <option value="pepperoni">Pepperoni</option>
        <option value="meaty">Meaty</option>
        <option value="cheesey">Cheesey</option>
      </select>
    </label>
    <button type="submit">
      Submit
    </button>
  </form>
`;

const form = document.forms.order;

function handleSubmit(event) {
  event.preventDefault();
  console.log(new FormData(event.target));
}

function handleFormData(event) {
  console.log(Array.from(event.formData)); /**[["fullname", "john"], ["pizza", "pepperoni"]] */
  console.log(Array.from(event.formData.values()));// ["john", "pepperoni"]
  console.log(event.formData)
  const entries = event.formData.entries();
  for (const entry of entries) {
    console.log(entry);
  }
}

form.addEventListener(‘submit‘, handleSubmit);
form.addEventListener(‘formdata‘, handleFormData);

 

[HTML 5] FormData

原文:https://www.cnblogs.com/Answer1215/p/13159327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!