質問するログイン新規登録

回答編集履歴

1

FormData

2017/08/16 14:02

投稿

think49
think49

スコア18194

answer CHANGED
@@ -1,13 +1,84 @@
1
+ ### HTMLFormElement.elements
2
+
1
3
  見たところ、`[name="database"]` は `.gpa` 直下にしか存在しないので、classで制限するまでもなく、下記コードでいいのではないでしょうか。
2
4
 
3
5
  ```JavaScript
4
6
  document.getElementById('insert_form').elements['database']
5
7
  ```
6
8
 
9
+ ### querySelectorAll
10
+
7
11
  class で制限するなら、`querySelectorAll` を使ってください。
8
12
 
9
13
  ```JavaScript
10
14
  document.querySelectorAll('.gpa>[name="database"]')
11
15
  ```
12
16
 
17
+ ### FormData
18
+
19
+ IE11- 用に Polyfill が必要ですが、FormData を使うと、楽ですね。
20
+
21
+ - [jimmywarting/FormData: HTML5 `FormData` polyfill for Browsers.](https://github.com/jimmywarting/FormData)
22
+
23
+ ```HTML
24
+ <form method="POST" name="insert" action="insert.php" onsubmit="count()" id="insert_form">
25
+ <table id="insert">
26
+ <thead>
27
+ <tr>
28
+ <th>科目情報</th>
29
+ <th>成績評価</th>
30
+ </tr>
31
+ </thead>
32
+ <tbody>
33
+ <tr>
34
+ <td>データベース</td>
35
+ <td class="gpa">
36
+ <input type="radio" id="zero_database" name="database" value="0" checked>
37
+ <label for="zero_database">不可</label>
38
+ <input type="radio" id="one_database" name="database" value="1">
39
+ <label for="one_database">可</label>
40
+ <input type="radio" id="two_database" name="database" value="2">
41
+ <label for="two_database">良</label>
42
+ <input type="radio" id="three_database" name="database" value="3">
43
+ <label for="three_database">優</label>
44
+ <input type="radio" id="four_database" name="database" value="4">
45
+ <label for="four_database">秀</label>
46
+ <input type="radio" id="none_database" name="database">
47
+ <label for="none_database">取得していない</label>
48
+
49
+ <input type="hidden" class="count" value="2">
50
+ </td>
51
+ </tr>
52
+ <tr>
53
+ <td>微積分</td>
54
+ <td class="gpa">
55
+ <input type="radio" id="zero_Calculus" name="Calculus" value="0" checked>
56
+ <label for="zero_Calculus">不可</label>
57
+ <input type="radio" id="one_Calculus" name="Calculus" value="1">
58
+ <label for="one_Calculus">可</label>
59
+ <input type="radio" id="two_Calculus" name="Calculus" value="2">
60
+ <label for="two_Calculus">良</label>
61
+ <input type="radio" id="three_Calculus" name="Calculus" value="3">
62
+ <label for="three_Calculus">優</label>
63
+ <input type="radio" id="four_Calculus" name="Calculus" value="4">
64
+ <label for="four_Calculus">秀</label>
65
+ <input type="radio" id="none_Calculus" name="Calculus">
66
+ <label for="none_Calculus">取得していない</label>
67
+
68
+ <input type="hidden" class="count" value="1">
69
+ </td>
70
+ </tr>
71
+ </tbody>
72
+ </table>
73
+ <input type="submit" value="送信">
74
+ </form>
75
+
76
+ <script>
77
+ 'use strict';
78
+ var formData = new FormData(document.getElementById('insert_form'));
79
+ console.log(formData.get('database')); // "0"
80
+ console.log(formData.get('Calculus')); // "0"
81
+ </script>
82
+ ```
83
+
13
84
  Re: imaharu さん