回答編集履歴

5

修正

2017/08/30 04:45

投稿

s8_chu
s8_chu

スコア14731

test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
 
60
60
 
61
- document.addEventListener("DOMContentLoaded", func);
61
+ window.addEventListener("load", func);
62
62
 
63
63
  check.addEventListener("change", func);
64
64
 
@@ -144,7 +144,7 @@
144
144
 
145
145
 
146
146
 
147
- document.addEventListener("DOMContentLoaded", func);
147
+ window.addEventListener("load", func);
148
148
 
149
149
  check.addEventListener("change", func);
150
150
 
@@ -212,11 +212,11 @@
212
212
 
213
213
 
214
214
 
215
- document.addEventListener("DOMContentLoaded", func);
216
-
217
- $("#Select").addEventListener("change", func);
215
+ $("#Select").on("change", func);
218
-
216
+
219
- document.querySelector("select[name^=test]").addEventListener("change", func);
217
+ $("select[name^=test]").on("change", func);
218
+
219
+ $(window).on("load", func);
220
220
 
221
221
  </script>
222
222
 

4

追記

2017/08/30 04:45

投稿

s8_chu
s8_chu

スコア14731

test CHANGED
@@ -224,4 +224,4 @@
224
224
 
225
225
  </html>
226
226
 
227
- `
227
+ ```

3

追記

2017/08/30 04:07

投稿

s8_chu
s8_chu

スコア14731

test CHANGED
@@ -157,3 +157,71 @@
157
157
  </html>
158
158
 
159
159
  ```
160
+
161
+ ```HTML
162
+
163
+ <!DOCTYPE html>
164
+
165
+ <html lang="ja">
166
+
167
+ <head>
168
+
169
+ <meta charset="UTF-8">
170
+
171
+ <title>タイトル</title>
172
+
173
+ </head>
174
+
175
+ <body>
176
+
177
+ <input type="checkbox" id="Select">
178
+
179
+ <?php for ($i = 0; $i < 3; $i++) : ?>
180
+
181
+ <select name="test[<?= $i ?>]" class="test">
182
+
183
+ <option value="">選択してね</option>
184
+
185
+ <?php for ($j = 0; $j < 3; $j++) : ?>
186
+
187
+ <option value="<?= $j ?>"><?= $j ?></option>
188
+
189
+ <?php endfor; ?>
190
+
191
+ </select>
192
+
193
+ <?php endfor; ?>
194
+
195
+ <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
196
+
197
+ <script>
198
+
199
+ function func(e) {
200
+
201
+ if ($("#Select").prop("checked")) {
202
+
203
+ $("select[name^=test]:not(:nth-of-type(1))").prop("selectedIndex", $("select[name^=test]").first().prop("selectedIndex"));
204
+
205
+ } else if (e.target.id === "Select") {
206
+
207
+ $("select[name^=test]").prop("selectedIndex", 0);
208
+
209
+ }
210
+
211
+ }
212
+
213
+
214
+
215
+ document.addEventListener("DOMContentLoaded", func);
216
+
217
+ $("#Select").addEventListener("change", func);
218
+
219
+ document.querySelector("select[name^=test]").addEventListener("change", func);
220
+
221
+ </script>
222
+
223
+ </body>
224
+
225
+ </html>
226
+
227
+ `

2

追記

2017/08/30 04:06

投稿

s8_chu
s8_chu

スコア14731

test CHANGED
@@ -71,3 +71,89 @@
71
71
  </html>
72
72
 
73
73
  ```
74
+
75
+ 追記
76
+
77
+ ---
78
+
79
+ ```HTML
80
+
81
+ <!DOCTYPE html>
82
+
83
+ <html lang="ja">
84
+
85
+ <head>
86
+
87
+ <meta charset="UTF-8">
88
+
89
+ <title>タイトル</title>
90
+
91
+ </head>
92
+
93
+ <body>
94
+
95
+ <input type="checkbox" id="Select">
96
+
97
+ <?php for ($i = 0; $i < 3; $i++) : ?>
98
+
99
+ <select name="test[<?= $i ?>]" class="test">
100
+
101
+ <option value="">選択してね</option>
102
+
103
+ <?php for ($j = 0; $j < 3; $j++) : ?>
104
+
105
+ <option value="<?= $j ?>"><?= $j ?></option>
106
+
107
+ <?php endfor; ?>
108
+
109
+ </select>
110
+
111
+ <?php endfor; ?>
112
+
113
+ <script>
114
+
115
+ var check = document.getElementById("Select");
116
+
117
+
118
+
119
+ function func(e) {
120
+
121
+ if (check.checked) {
122
+
123
+ var select1 = document.querySelectorAll("select[name^=test]:not(:nth-of-type(1))");
124
+
125
+ for (var i = 0; i < select1.length; i++) {
126
+
127
+ select1[i].selectedIndex = document.querySelector("select[name^=test]").selectedIndex;
128
+
129
+ }
130
+
131
+ } else if (e.target.id === "Select") {
132
+
133
+ var select2 = document.querySelectorAll("select[name^=test]");
134
+
135
+ for (var j = 0; j < select2.length; j++) {
136
+
137
+ select2[j].selectedIndex = 0;
138
+
139
+ }
140
+
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ document.addEventListener("DOMContentLoaded", func);
148
+
149
+ check.addEventListener("change", func);
150
+
151
+ document.querySelector("select[name^=test]").addEventListener("change", func);
152
+
153
+ </script>
154
+
155
+ </body>
156
+
157
+ </html>
158
+
159
+ ```

1

修正

2017/08/30 03:52

投稿

s8_chu
s8_chu

スコア14731

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  <!DOCTYPE html>
6
6
 
7
- <html>
7
+ <html lang="ja">
8
8
 
9
9
  <head>
10
10