質問編集履歴

1

ソース追加

2018/02/28 09:31

投稿

panda
panda

スコア20

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,142 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
+ ```html
26
+
27
+ <body>
28
+
29
+ <form>
30
+
31
+ <label><input type="radio" name="move" value="bus" id="kind-1">バス</label>
32
+
33
+ <label><input type="radio" name="move" value="train">電車</label>
34
+
35
+ </form>
36
+
37
+ <form>
38
+
39
+ <div class="second" id="bus">
40
+
41
+ <label><input type="radio" name="grade" value="bus_high">バス/高級</label>
42
+
43
+ <label><input type="radio" name="grade" value="bus_normal">バス/普通</label>
44
+
45
+ </div>
46
+
47
+ <div class="second" id="train">
48
+
49
+ <label><input type="radio" name="grade" value="train_high">電車/高級</label>
50
+
51
+ <label><input type="radio" name="grade" value="train_normal">電車/普通</label>
52
+
53
+ </div>
54
+
55
+ </form>
56
+
57
+
58
+
59
+ <div class="third" id="bus_high">高級バス</div>
60
+
61
+ <div class="third" id="bus_normal">バス</div>
62
+
63
+ <div class="third" id="train_high">高級電車</div>
64
+
65
+ <div class="third" id="train_normal">電車</div>
66
+
67
+ </body>
68
+
69
+
70
+
71
+ ```
72
+
73
+
74
+
75
+ ```css
76
+
77
+ .second , .third{
78
+
79
+ display: none;
80
+
81
+ }
82
+
83
+ ```
84
+
85
+
86
+
87
+ ```js
88
+
89
+ // ラジオボタンで表示/非表示
90
+
91
+ document.addEventListener('change',function(e){
92
+
93
+ var t=e.target;
94
+
95
+ if(t.name=="status") document.querySelector('#'+t.value).checked=true;
96
+
97
+ });
98
+
99
+
100
+
101
+
102
+
103
+ // リンククリックでラジオボタンにチェック入れる
104
+
105
+ window.addEventListener('DOMContentLoaded', function(e){
106
+
107
+ document.querySelector(location.hash).checked=true;
108
+
109
+ });
110
+
111
+
112
+
113
+
114
+
115
+ // ラジオボタン2段階で表示
116
+
117
+ document.addEventListener('change',function(e){
118
+
119
+ var t=e.target;
120
+
121
+ if(t.name=="move"){
122
+
123
+ Array.prototype.forEach.call(document.querySelectorAll('.second,.third'),function(x){
124
+
125
+ if(x.id==t.value){
126
+
127
+ x.style.display="block";
128
+
129
+ Array.prototype.forEach.call(x.querySelectorAll('[type=radio]'),function(y){
130
+
131
+ y.checked=false;
132
+
133
+ });
134
+
135
+ }else{
136
+
137
+ x.style.display="none";
138
+
139
+ }
140
+
141
+ });
142
+
143
+ }
144
+
145
+ if(t.name=="grade"){
146
+
147
+ Array.prototype.forEach.call(document.querySelectorAll('.third'),function(x){
148
+
149
+ x.style.display=(x.id==t.value)?"block":"none";
150
+
151
+ });
152
+
153
+ }
154
+
155
+ });
156
+
157
+
158
+
159
+ ```
160
+
25
161
 
26
162
 
27
163
  ソースは以前回答していただいたものをそのまま使用しています。