Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

javascript - How can I get all the data from a summited form and make that into a new list

I want to take all the data submitted and make it into a list ( -item: bannana -itemNum: #3330...)

<form id="form" action="">
  <h3>Item:</h3>
  <input name="item" type="text">
  <h3>Item numbers:</h3>
  <input name="itemNum" type="number">
  <h3>Quantity:</h3>
  <input name="quantity" type="number">
  <h3> Price:</h3>
  <input name="price" type="number">
  <h3>Discount:</h3>
  <input name="discount" type="number">
  <br>
  <button id="but">Submit</button>
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just one line:

let formInputs = Object.fromEntries( new FormData(myForm).entries() ) 

see FormData

in context:

myForm = document.querySelector('#myForm')

myForm.onsubmit = evt =>
  {
  evt.preventDefault() // disable submit   page reload
  console.clear()
 
  let formInputs = Object.fromEntries( new FormData(myForm).entries() ) 

  console.log( formInputs )
}
body, textarea, input {
  font-family : Helvetica, Arial sans-serif;
  font-size   : 12px;
  }
label {
  font-size   : 1.2em;
  display     : block;
  float       : left;
  clear       : both;
  width       : 10em;
  white-space : nowrap;
  overflow    : hidden;
  font-weight : bold;
  padding-top : .7em;
  line-height: 1.4em;
  }
label:after {
  content     : '....................................'; 
  font-weight : normal;
  color       : lightslategray;
  }
input {
  display     : block;
  float       : left;
  width       : 5em;
  margin      : .3em;
  }
button {
  display     : block;
  float       : left;
  clear       : both;
  margin-top  : .6em;
  }
.as-console-wrapper { max-height:100% !important; top:0; left:50% !important; width:50%; }
.as-console-row         { background-color: yellow; }
.as-console-row::after  { display:none !important; }
<form id="myForm" action="">
  <label> Item:</label>         <input name="item"     type="text"   >
  <label> Item numbers:</label> <input name="itemNum"  type="number" >
  <label> Quantity:</label>     <input name="quantity" type="number" >
  <label> Price:</label>        <input name="price"    type="number" >
  <label> Discount:</label>     <input name="discount" type="number" >
 
  <button type="submit">Submit</button>
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...