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

質問編集履歴

1

ソース追加

2018/02/28 09:31

投稿

panda
panda

スコア20

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,75 @@
10
10
  2段階目のラジオボタンが表示されないです。
11
11
 
12
12
  ### 該当のソースコード
13
+ ```html
14
+ <body>
15
+ <form>
16
+ <label><input type="radio" name="move" value="bus" id="kind-1">バス</label>
17
+ <label><input type="radio" name="move" value="train">電車</label>
18
+ </form>
19
+ <form>
20
+ <div class="second" id="bus">
21
+ <label><input type="radio" name="grade" value="bus_high">バス/高級</label>
22
+ <label><input type="radio" name="grade" value="bus_normal">バス/普通</label>
23
+ </div>
24
+ <div class="second" id="train">
25
+ <label><input type="radio" name="grade" value="train_high">電車/高級</label>
26
+ <label><input type="radio" name="grade" value="train_normal">電車/普通</label>
27
+ </div>
28
+ </form>
13
29
 
30
+ <div class="third" id="bus_high">高級バス</div>
31
+ <div class="third" id="bus_normal">バス</div>
32
+ <div class="third" id="train_high">高級電車</div>
33
+ <div class="third" id="train_normal">電車</div>
34
+ </body>
35
+
36
+ ```
37
+
38
+ ```css
39
+ .second , .third{
40
+ display: none;
41
+ }
42
+ ```
43
+
44
+ ```js
45
+ // ラジオボタンで表示/非表示
46
+ document.addEventListener('change',function(e){
47
+ var t=e.target;
48
+ if(t.name=="status") document.querySelector('#'+t.value).checked=true;
49
+ });
50
+
51
+
52
+ // リンククリックでラジオボタンにチェック入れる
53
+ window.addEventListener('DOMContentLoaded', function(e){
54
+ document.querySelector(location.hash).checked=true;
55
+ });
56
+
57
+
58
+ // ラジオボタン2段階で表示
59
+ document.addEventListener('change',function(e){
60
+ var t=e.target;
61
+ if(t.name=="move"){
62
+ Array.prototype.forEach.call(document.querySelectorAll('.second,.third'),function(x){
63
+ if(x.id==t.value){
64
+ x.style.display="block";
65
+ Array.prototype.forEach.call(x.querySelectorAll('[type=radio]'),function(y){
66
+ y.checked=false;
67
+ });
68
+ }else{
69
+ x.style.display="none";
70
+ }
71
+ });
72
+ }
73
+ if(t.name=="grade"){
74
+ Array.prototype.forEach.call(document.querySelectorAll('.third'),function(x){
75
+ x.style.display=(x.id==t.value)?"block":"none";
76
+ });
77
+ }
78
+ });
79
+
80
+ ```
81
+
14
82
  ソースは以前回答していただいたものをそのまま使用しています。
15
83
 
16
84
  ### イメージ画像