回答編集履歴

3

ちょうせい

2021/03/20 11:25

投稿

yambejp
yambejp

スコア116683

test CHANGED
@@ -81,3 +81,95 @@
81
81
  </table>
82
82
 
83
83
  ```
84
+
85
+ # 上限あり
86
+
87
+ ```javascript
88
+
89
+ <script>
90
+
91
+ window.addEventListener('DOMContentLoaded', ()=>{
92
+
93
+ const max=3;
94
+
95
+ const sub=document.querySelector('#sub');
96
+
97
+ document.querySelector('#btn').addEventListener('click',()=>{
98
+
99
+ if(sub.value=="notselect"){
100
+
101
+ alert('not selected!');
102
+
103
+ }else if([...document.querySelectorAll('#sublist tbody td')].map(x=>x.textContent).includes(sub.value)){
104
+
105
+ alert('already exists');
106
+
107
+ }else if([...document.querySelectorAll('#sublist tbody td')].length>=max){
108
+
109
+ alert(`can't regist anymore`);
110
+
111
+ }else{
112
+
113
+ document.querySelector('#sublist tbody').appendChild(
114
+
115
+ [Object.assign(document.createElement('td'),{textContent:sub.value})].reduce((x,y)=>{
116
+
117
+ x.appendChild(y);
118
+
119
+ return x;
120
+
121
+ },document.createElement('tr'))
122
+
123
+ );
124
+
125
+ }
126
+
127
+ });
128
+
129
+ });
130
+
131
+ </script>
132
+
133
+
134
+
135
+ <select id="sub">
136
+
137
+ <option value="notselect"></option>
138
+
139
+ <option value="01302年現代文Ba・月3水1">0130 2年現代文Ba・月3水1</option>
140
+
141
+ <option value="01312年現代文Bb・月4水2">0131 2年現代文Bb・月4水2</option>
142
+
143
+ <option value="01322年現代文Bc・月4金1">0132 2年現代文Bc・月4金1</option>
144
+
145
+ <option value="01332年現代文Bd・火2木2">0133 2年現代文Bd・火2木2</option>
146
+
147
+ <option value="01342年現代文Be・火4木2">0134 2年現代文Be・火4木2</option>
148
+
149
+ <option value="01352年現代文Bf・水1木4">0135 2年現代文Bf・水1木4</option>
150
+
151
+ <option value="01362年現代文Bg・水2金1">0136 2年現代文Bg・水2金1</option>
152
+
153
+ </select>
154
+
155
+ <button id="btn">追加</button>
156
+
157
+ <table border="1" id="sublist">
158
+
159
+ <thead>
160
+
161
+ <tr>
162
+
163
+ <th>科目リスト</th>
164
+
165
+ </tr>
166
+
167
+ </thead>
168
+
169
+ <tbody>
170
+
171
+ </tbody>
172
+
173
+ </table>
174
+
175
+ ```

2

chousei

2021/03/20 11:25

投稿

yambejp
yambejp

スコア116683

test CHANGED
@@ -81,135 +81,3 @@
81
81
  </table>
82
82
 
83
83
  ```
84
-
85
- # 調整版
86
-
87
- ```jaascript
88
-
89
- <script>
90
-
91
- window.addEventListener('DOMContentLoaded', ()=>{
92
-
93
- const s1=document.querySelector('#s1');
94
-
95
- const s2=s1.cloneNode(true);
96
-
97
- const p=s1.parentNode;
98
-
99
- s2.setAttribute('id','s2');
100
-
101
- s2.querySelectorAll('.hide').forEach(x=>x.remove());
102
-
103
- document.querySelector('#color').addEventListener('change',()=>{
104
-
105
- if([...p.children].includes(s1)){
106
-
107
- p.insertBefore(s2,s1);
108
-
109
- s1.remove();
110
-
111
- }else{
112
-
113
- p.insertBefore(s1,s2);
114
-
115
- s2.remove();
116
-
117
- }
118
-
119
- });
120
-
121
- document.addEventListener('change',()=>{
122
-
123
- const color=document.querySelector('#s1,#s2').value;
124
-
125
- document.querySelectorAll('.s1_select').forEach(x=>{
126
-
127
- x.style.display='none';
128
-
129
- x.querySelector('select').disabled=true;
130
-
131
- });
132
-
133
- document.querySelector('#'+color).style.display='';
134
-
135
- document.querySelector('#'+color+" select").disabled=false;
136
-
137
- ;
138
-
139
- });
140
-
141
- });
142
-
143
- </script>
144
-
145
- <label><input type="checkbox" id="color" name="color"> カラー</label>
146
-
147
- <label>
148
-
149
- <select id="s1">
150
-
151
- <option value="white" class="hide">白</option>
152
-
153
- <option value="red">赤</option>
154
-
155
- <option value="black" class="hide">黒</option>
156
-
157
- </select>
158
-
159
- </label>
160
-
161
-
162
-
163
- <div class="s1_select" id="white">
164
-
165
- <label>
166
-
167
- <select>
168
-
169
- <option value="white1">WHITE1</option>
170
-
171
- <option value="white2">WHITE2</option>
172
-
173
- </select>
174
-
175
- </label>
176
-
177
- </div>
178
-
179
-
180
-
181
- <div class="s1_select" id="red" style="display:none">
182
-
183
- <label>
184
-
185
- <select disabled>
186
-
187
- <option value="red1">RED1</option>
188
-
189
- <option value="red2">RED2</option>
190
-
191
- </select>
192
-
193
- </label>
194
-
195
- </div>
196
-
197
-
198
-
199
- <div class="s1_select" id="black" style="display:none">
200
-
201
- <label>
202
-
203
- <select disabled>
204
-
205
- <option value="black1">BLACK1</option>
206
-
207
- <option value="black2">BLACK2</option>
208
-
209
- </select>
210
-
211
- </label>
212
-
213
- </div>
214
-
215
- ```

1

chousei

2021/03/20 07:51

投稿

yambejp
yambejp

スコア116683

test CHANGED
@@ -81,3 +81,135 @@
81
81
  </table>
82
82
 
83
83
  ```
84
+
85
+ # 調整版
86
+
87
+ ```jaascript
88
+
89
+ <script>
90
+
91
+ window.addEventListener('DOMContentLoaded', ()=>{
92
+
93
+ const s1=document.querySelector('#s1');
94
+
95
+ const s2=s1.cloneNode(true);
96
+
97
+ const p=s1.parentNode;
98
+
99
+ s2.setAttribute('id','s2');
100
+
101
+ s2.querySelectorAll('.hide').forEach(x=>x.remove());
102
+
103
+ document.querySelector('#color').addEventListener('change',()=>{
104
+
105
+ if([...p.children].includes(s1)){
106
+
107
+ p.insertBefore(s2,s1);
108
+
109
+ s1.remove();
110
+
111
+ }else{
112
+
113
+ p.insertBefore(s1,s2);
114
+
115
+ s2.remove();
116
+
117
+ }
118
+
119
+ });
120
+
121
+ document.addEventListener('change',()=>{
122
+
123
+ const color=document.querySelector('#s1,#s2').value;
124
+
125
+ document.querySelectorAll('.s1_select').forEach(x=>{
126
+
127
+ x.style.display='none';
128
+
129
+ x.querySelector('select').disabled=true;
130
+
131
+ });
132
+
133
+ document.querySelector('#'+color).style.display='';
134
+
135
+ document.querySelector('#'+color+" select").disabled=false;
136
+
137
+ ;
138
+
139
+ });
140
+
141
+ });
142
+
143
+ </script>
144
+
145
+ <label><input type="checkbox" id="color" name="color"> カラー</label>
146
+
147
+ <label>
148
+
149
+ <select id="s1">
150
+
151
+ <option value="white" class="hide">白</option>
152
+
153
+ <option value="red">赤</option>
154
+
155
+ <option value="black" class="hide">黒</option>
156
+
157
+ </select>
158
+
159
+ </label>
160
+
161
+
162
+
163
+ <div class="s1_select" id="white">
164
+
165
+ <label>
166
+
167
+ <select>
168
+
169
+ <option value="white1">WHITE1</option>
170
+
171
+ <option value="white2">WHITE2</option>
172
+
173
+ </select>
174
+
175
+ </label>
176
+
177
+ </div>
178
+
179
+
180
+
181
+ <div class="s1_select" id="red" style="display:none">
182
+
183
+ <label>
184
+
185
+ <select disabled>
186
+
187
+ <option value="red1">RED1</option>
188
+
189
+ <option value="red2">RED2</option>
190
+
191
+ </select>
192
+
193
+ </label>
194
+
195
+ </div>
196
+
197
+
198
+
199
+ <div class="s1_select" id="black" style="display:none">
200
+
201
+ <label>
202
+
203
+ <select disabled>
204
+
205
+ <option value="black1">BLACK1</option>
206
+
207
+ <option value="black2">BLACK2</option>
208
+
209
+ </select>
210
+
211
+ </label>
212
+
213
+ </div>
214
+
215
+ ```