回答編集履歴

3

修正

2018/04/18 02:44

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -253,3 +253,147 @@
253
253
  </div>
254
254
 
255
255
  ```
256
+
257
+
258
+
259
+ # imgを使わない
260
+
261
+
262
+
263
+ ちょっと誤解していました、あくまでspanのみで画像を処理しているのですね
264
+
265
+ ではこんな感じで
266
+
267
+ ※今回はとりあえずcssにはbackground-colorを入れておきます
268
+
269
+ ```CSS
270
+
271
+ .pink{background-Color:pink;}
272
+
273
+ .aqua{background-Color:aqua;}
274
+
275
+ .i{display:block;border:1px solid;height:100px;width:100px;}
276
+
277
+ .apple{background-Color:red;}
278
+
279
+ .grape{background-Color:fuchsia;}
280
+
281
+ .banana{background-Color:yellow;}
282
+
283
+ .kiwi{background-Color:lime;}
284
+
285
+ ```
286
+
287
+ ```javascript
288
+
289
+ $(function(){
290
+
291
+ $('input[name=check_fluits]').change(function(){
292
+
293
+ var clone=$(this).siblings().find('.i').clone();
294
+
295
+ if($('input[name=check_fluits]:checked').length>2) {
296
+
297
+ $(this).prop("checked",false);
298
+
299
+ alert('もうダメ!');
300
+
301
+ return false;
302
+
303
+ }
304
+
305
+ if($(this).prop('checked')){
306
+
307
+ $('#output1,#output2').filter(function(){
308
+
309
+ return $(this).html()=="";
310
+
311
+ }).eq(0).html(clone);
312
+
313
+ }else{
314
+
315
+ console.log($.map(clone.prop('classList'),function(x){return "."+x;}).join(""));
316
+
317
+ //console.log(clone.prop('classList').map(function(){return "."+$(this);}));
318
+
319
+ $('#output1,#output2').has($.map(clone.prop('classList'),function(x){return "."+x;}).join("")).html("");
320
+
321
+ }
322
+
323
+ });
324
+
325
+ $('input[name=check_fluits]').prop('checked',false).filter('[value=apple],[value=banana]').prop('checked',true).trigger("change");
326
+
327
+ });
328
+
329
+
330
+
331
+ ```
332
+
333
+ ```HTML
334
+
335
+ <div class="check_wrap">
336
+
337
+ <ul class="list">
338
+
339
+ <li><input id="fluits_apple" class="check_fluits" name="check_fluits" value="apple" type="checkbox">
340
+
341
+ <label for="fluits_apple" class="check_label">
342
+
343
+ <span class="i apple"></span>
344
+
345
+ </label>
346
+
347
+ </li>
348
+
349
+ <li><input id="fluits_grape" class="check_fluits" name="check_fluits" value="grape" type="checkbox">
350
+
351
+ <label for="fluits_grape" class="check_label">
352
+
353
+ <span class="i grape"></span>
354
+
355
+ </label>
356
+
357
+ </li>
358
+
359
+ <li><input id="fluits_banana" class="check_fluits" name="check_fluits" value="banana" type="checkbox">
360
+
361
+ <label for="fluits_banana" class="check_label">
362
+
363
+ <span class="i banana"></span>
364
+
365
+ </label>
366
+
367
+ </li>
368
+
369
+ <li><input id="fluits_kiwi" class="check_fluits" name="check_fluits" value="kiwi" type="checkbox">
370
+
371
+ <label for="fluits_kiwi" class="check_label">
372
+
373
+ <span class="i kiwi"></span>
374
+
375
+ </label>
376
+
377
+ </li>
378
+
379
+ </ul>
380
+
381
+ </div>
382
+
383
+
384
+
385
+
386
+
387
+ <div class="output">
388
+
389
+ <p class="pink">1つ目のチェック画像を、1つだけピンクの中にクローン
390
+
391
+ <span id="output1"></span></p>
392
+
393
+ <p class="aqua">2つ目のチェック画像を、1つだけ水色の中にクローン
394
+
395
+ <span id="output2"></span></p>
396
+
397
+ </div>
398
+
399
+ ```

2

改定

2018/04/18 02:44

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -127,3 +127,129 @@
127
127
  </div>
128
128
 
129
129
  ```
130
+
131
+
132
+
133
+ # 改訂版
134
+
135
+ ざっと仕様を入れ込んでみました
136
+
137
+ もう少しスマートにできそうですがとりあえず動くとは思います
138
+
139
+ ```CSS
140
+
141
+ .pink{background-Color:pink;}
142
+
143
+ .aqua{background-Color:aqua;}
144
+
145
+
146
+
147
+ ```
148
+
149
+ ```javascript
150
+
151
+ $(function(){
152
+
153
+ $('input[name=check_fluits]').change(function(){
154
+
155
+ var clone=$(this).siblings().find('.i>img').clone();
156
+
157
+ if($('input[name=check_fluits]:checked').length>2) {
158
+
159
+ $(this).prop("checked",false);
160
+
161
+ alert('もうダメ!');
162
+
163
+ return false;
164
+
165
+ }
166
+
167
+ if($(this).prop('checked')){
168
+
169
+ $('#output1,#output2').filter(function(){
170
+
171
+ return $(this).html()=="";
172
+
173
+ }).eq(0).html(clone);
174
+
175
+ }else{
176
+
177
+ $('#output1,#output2').has('img[src="'+clone.attr('src')+'"]').html("");
178
+
179
+ }
180
+
181
+ });
182
+
183
+ $('input[name=check_fluits]').prop('checked',false).filter('[value=apple],[value=banana]').prop('checked',true).trigger("change");
184
+
185
+ });
186
+
187
+ ```
188
+
189
+ ```html
190
+
191
+ <div class="check_wrap">
192
+
193
+ <ul class="list">
194
+
195
+ <li><input id="fluits_apple" class="check_fluits" name="check_fluits" value="apple" type="checkbox">
196
+
197
+ <label for="fluits_apple" class="check_label">
198
+
199
+ <span class="i apple"><img src="apple.jpg" alt="りんご"></span>
200
+
201
+ </label>
202
+
203
+ </li>
204
+
205
+ <li><input id="fluits_grape" class="check_fluits" name="check_fluits" value="grape" type="checkbox">
206
+
207
+ <label for="fluits_grape" class="check_label">
208
+
209
+ <span class="i grape"><img src="grape.jpg" alt="ぶどう"></span>
210
+
211
+ </label>
212
+
213
+ </li>
214
+
215
+ <li><input id="fluits_banana" class="check_fluits" name="check_fluits" value="banana" type="checkbox">
216
+
217
+ <label for="fluits_banana" class="check_label">
218
+
219
+ <span class="i banana"><img src="banana.jpg" alt="バナナ"></span>
220
+
221
+ </label>
222
+
223
+ </li>
224
+
225
+ <li><input id="fluits_kiwi" class="check_fluits" name="check_fluits" value="kiwi" type="checkbox">
226
+
227
+ <label for="fluits_kiwi" class="check_label">
228
+
229
+ <span class="i kiwi"><img src="kiwi.jpg" alt="キウイ"></span>
230
+
231
+ </label>
232
+
233
+ </li>
234
+
235
+ </ul>
236
+
237
+ </div>
238
+
239
+
240
+
241
+
242
+
243
+ <div class="output">
244
+
245
+ <p class="pink">1つ目のチェック画像を、1つだけピンクの中にクローン
246
+
247
+ <span id="output1"></span></p>
248
+
249
+ <p class="aqua">2つ目のチェック画像を、1つだけ水色の中にクローン
250
+
251
+ <span id="output2"></span></p>
252
+
253
+ </div>
254
+
255
+ ```

1

追記

2018/04/18 01:27

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -3,6 +3,18 @@
3
3
  とくに、2個目をチェックしたあとに、外して再度別のチェックを
4
4
 
5
5
  つけたりするとぼろぼろです
6
+
7
+ ```CSS
8
+
9
+ .pink{background-Color:pink;}
10
+
11
+ .aqua{background-Color:aqua;}
12
+
13
+
14
+
15
+ ```
16
+
17
+
6
18
 
7
19
  ```javascript
8
20