lang/js

VanillaJS Form serialize()

C/H 2019. 12. 16. 11:17
<form method="dialog">
  <p><label>Favorite animal:
    <select name="favAnimal" required>
      <option></option>
      <option>Brine shrimp</option>
      <option>Red panda</option>
      <option>Spider monkey</option>
    </select></label>
  </p>
  <p>
    <label>
      <input type="text" name="animallName" placeholder="animal name" value="Tiger" />
    </label>
  </p>
  <menu>
    <button>Cancel</button>
    <button>Confirm</button>
  </menu>
</form>
Array.from(document.querySelector('form')).map(item=>item.name?`${item.name}=${item.value}`:null).filter(item=>!!item).join('&')
// "favAnimal=&animallName=Tiger"
반응형