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

回答編集履歴

2

追記

2017/07/21 12:11

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -64,4 +64,135 @@
64
64
  </tbody>
65
65
  </table>
66
66
 
67
+ ```
68
+
69
+ # 追記
70
+ ラジオボタンとテーブルの組み合わせが複数ある場合は、formでグルーピングしてください
71
+ ```javascript
72
+ window.onload = function(){
73
+ entryChange();
74
+ Array.prototype.map.call(document.querySelectorAll("[name=entryPlan]"),function(i){
75
+ i.addEventListener('change',function(){entryChange()});
76
+ });
77
+ }
78
+ function entryChange(){
79
+ var ele=document.querySelectorAll("[name=entryPlan]:checked");
80
+ for(var i=0;i<ele.length;i++){
81
+ v=ele[i].value;
82
+ Array.prototype.map.call(ele[i].form.querySelectorAll(".boxes"),function(j){
83
+ j.classList.add('hide');
84
+ if(
85
+ (v=='hoge1' && j.classList.contains('box1')) ||
86
+ (v=='hoge2' && j.classList.contains('box2')) ||
87
+ (v=='hoge3' && (j.classList.contains('box1') || j.classList.contains('box2')))
88
+ ){
89
+ j.classList.remove('hide');
90
+ }
91
+ });
92
+ }
93
+ }
94
+ ```
95
+
96
+ ```HTML
97
+ <div>切り替え</div>
98
+ <form>
99
+ <div>
100
+ <label><input type="radio" name="entryPlan" value="hoge1">1だけ表示</label>
101
+ <label><input type="radio" name="entryPlan" value="hoge2">23表示</label>
102
+ <label><input type="radio" name="entryPlan" value="hoge3">123表示</label>
103
+ </div>
104
+ <table>
105
+ <thead>
106
+ <tr>
107
+ <th>番号</th>
108
+ <th>食べ物</th>
109
+ <th>飲み物</th>
110
+ </tr>
111
+ </thead>
112
+ <tbody>
113
+ <tr class="boxes box1">
114
+ <th>1</th>
115
+ <th>ぶどう</th>
116
+ <th>ワイン</th>
117
+ </tr>
118
+ <tr class="boxes box2">
119
+ <th>2</th>
120
+ <th>ホップ</th>
121
+ <th>ビール</th>
122
+ </tr>
123
+ <tr class="boxes box2">
124
+ <th>3</th>
125
+ <th>ケーキ</th>
126
+ <th>チョコ</th>
127
+ </tr>
128
+ </tbody>
129
+ </table>
130
+ </form>
131
+ <form>
132
+ <div>
133
+ <label><input type="radio" name="entryPlan" value="hoge1">1だけ表示</label>
134
+ <label><input type="radio" name="entryPlan" value="hoge2">23表示</label>
135
+ <label><input type="radio" name="entryPlan" value="hoge3">123表示</label>
136
+ </div>
137
+ <table>
138
+ <thead>
139
+ <tr>
140
+ <th>番号</th>
141
+ <th>食べ物</th>
142
+ <th>飲み物</th>
143
+ </tr>
144
+ </thead>
145
+ <tbody>
146
+ <tr class="boxes box1">
147
+ <th>1</th>
148
+ <th>ぶどう</th>
149
+ <th>ワイン</th>
150
+ </tr>
151
+ <tr class="boxes box2">
152
+ <th>2</th>
153
+ <th>ホップ</th>
154
+ <th>ビール</th>
155
+ </tr>
156
+ <tr class="boxes box2">
157
+ <th>3</th>
158
+ <th>ケーキ</th>
159
+ <th>チョコ</th>
160
+ </tr>
161
+ </tbody>
162
+ </table>
163
+ </form>
164
+ <form>
165
+ <div>
166
+ <label><input type="radio" name="entryPlan" value="hoge1">1だけ表示</label>
167
+ <label><input type="radio" name="entryPlan" value="hoge2">23表示</label>
168
+ <label><input type="radio" name="entryPlan" value="hoge3">123表示</label>
169
+ </div>
170
+ <table>
171
+ <thead>
172
+ <tr>
173
+ <th>番号</th>
174
+ <th>食べ物</th>
175
+ <th>飲み物</th>
176
+ </tr>
177
+ </thead>
178
+ <tbody>
179
+ <tr class="boxes box1">
180
+ <th>1</th>
181
+ <th>ぶどう</th>
182
+ <th>ワイン</th>
183
+ </tr>
184
+ <tr class="boxes box2">
185
+ <th>2</th>
186
+ <th>ホップ</th>
187
+ <th>ビール</th>
188
+ </tr>
189
+ <tr class="boxes box2">
190
+ <th>3</th>
191
+ <th>ケーキ</th>
192
+ <th>チョコ</th>
193
+ </tr>
194
+ </tbody>
195
+ </table>
196
+ </form>
197
+
67
198
  ```

1

sample

2017/07/21 12:11

投稿

yambejp
yambejp

スコア117946

answer CHANGED
@@ -1,4 +1,67 @@
1
1
  まず、命題はチェックボックスといっていますがラジオボタンですね
2
2
  間違いはidを利用して複数箇所処理をしようとしていることです
3
3
  複数のタグに同じidを振ることはできませんので
4
- どうしてもということであればclassを利用して下さい
4
+ どうしてもということであればclassを利用して下さい
5
+
6
+ # sample
7
+ 一応サンプルつけておきます
8
+ ```CSS
9
+ .hide{display:none;}
10
+ ```
11
+ ```javascript
12
+ window.onload = function(){
13
+ entryChange();
14
+ Array.prototype.map.call(document.querySelectorAll("[name=entryPlan]"),function(i){
15
+ i.addEventListener('change',function(){entryChange()});
16
+ });
17
+ }
18
+ function entryChange(){
19
+ var ele=document.querySelectorAll("[name=entryPlan]:checked");
20
+ var v=ele.length>0?ele[0].value:null;
21
+ Array.prototype.map.call(document.querySelectorAll(".boxes"),function(i){
22
+ i.classList.add('hide');
23
+ if(
24
+ (v=='hoge1' && i.classList.contains('box1')) ||
25
+ (v=='hoge2' && i.classList.contains('box2')) ||
26
+ (v=='hoge3' && (i.classList.contains('box1') || i.classList.contains('box2')))
27
+ ){
28
+ i.classList.remove('hide');
29
+ }
30
+ });
31
+ }
32
+ ```
33
+ ```HTML
34
+ <div>切り替え</div>
35
+ <div>
36
+ <label><input type="radio" name="entryPlan" value="hoge1" checked>1だけ表示</label>
37
+ <label><input type="radio" name="entryPlan" value="hoge2">23表示</label>
38
+ <label><input type="radio" name="entryPlan" value="hoge3">123表示</label>
39
+ </div>
40
+ <table>
41
+ <thead>
42
+ <tr>
43
+ <th>番号</th>
44
+ <th>食べ物</th>
45
+ <th>飲み物</th>
46
+ </tr>
47
+ </thead>
48
+ <tbody>
49
+ <tr class="boxes box1">
50
+ <th>1</th>
51
+ <th>ぶどう</th>
52
+ <th>ワイン</th>
53
+ </tr>
54
+ <tr class="boxes box2">
55
+ <th>2</th>
56
+ <th>ホップ</th>
57
+ <th>ビール</th>
58
+ </tr>
59
+ <tr class="boxes box2">
60
+ <th>3</th>
61
+ <th>ケーキ</th>
62
+ <th>チョコ</th>
63
+ </tr>
64
+ </tbody>
65
+ </table>
66
+
67
+ ```