0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptにおける、formのループ

Last updated at Posted at 2020-05-03

以下のようなformのHTMLがあったときに

  <form id="formdata">
    <input type="text" value="" name="text1">
    <input type="text" value="" name="text2">
    <select name="select1" >
      <option value="option1">option1</option>
      <option value="option2">option2</option>
      <option value="option3">option3</option>
    </select>
  </form>

form内でループするには

// formタグを取得
var formdata = document.getElementById("formdata");
// データ分のループ
for (var idx = 0; idx < formdata.elements.length; idx++) {
    // 値を取りたいとき
    var formvalue = formdata.elements[idx].value;
    // nameを取りたいとき
    var formname = formdata.elements[idx].name;
    // console.log(formvalue);
    // console.log(formname);
}

同じnameがあるとき(nameが配列になる時は、考慮が必要なので注意)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?