回答編集履歴
3
無駄な判断のような気がするので1回だけするようにした。
test
CHANGED
@@ -8,9 +8,11 @@
|
|
8
8
|
|
9
9
|
var products = document.getElementsByClassName(productClass);
|
10
10
|
|
11
|
+
var displayStyle = company.checked ? "block" : "none";
|
12
|
+
|
11
13
|
Array.prototype.forEach.call(products, function(product) {
|
12
14
|
|
13
|
-
product.style.display =
|
15
|
+
product.style.display = displayStyle;
|
14
16
|
|
15
17
|
});
|
16
18
|
|
@@ -18,6 +20,4 @@
|
|
18
20
|
|
19
21
|
}
|
20
22
|
|
21
|
-
|
22
|
-
|
23
23
|
```
|
2
変更
test
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
function dsProduct() {
|
4
4
|
|
5
|
-
|
5
|
+
document.getElementsByName("company").forEach(function(company) {
|
6
|
-
|
7
|
-
companies.forEach(function(company) {
|
8
6
|
|
9
7
|
var productClass = company.value.replace("company", "product");
|
10
8
|
|
1
変更
test
CHANGED
@@ -1,69 +1,3 @@
|
|
1
|
-
元のコードをベースにすると、以下のような感じでできると思います。
|
2
|
-
|
3
|
-
```JavaScript
|
4
|
-
|
5
|
-
function dsProduct() {
|
6
|
-
|
7
|
-
var products = document.getElementsByName("company");
|
8
|
-
|
9
|
-
var productAList = document.getElementsByClassName("productA");
|
10
|
-
|
11
|
-
Array.prototype.forEach.call(productAList, function(v) {
|
12
|
-
|
13
|
-
v.style.display = products[0].checked ? "block" : "none";
|
14
|
-
|
15
|
-
});
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
var productBList = document.getElementsByClassName("productB");
|
20
|
-
|
21
|
-
Array.prototype.forEach.call(productBList, function(v) {
|
22
|
-
|
23
|
-
v.style.display = products[1].checked ? "block" : "none";
|
24
|
-
|
25
|
-
});
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
var productCList = document.getElementsByClassName("productC");
|
30
|
-
|
31
|
-
Array.prototype.forEach.call(productCList, function(v) {
|
32
|
-
|
33
|
-
v.style.display = products[2].checked ? "block" : "none";
|
34
|
-
|
35
|
-
});
|
36
|
-
|
37
|
-
}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
```
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
HTMLにidを加えてよければ、もう少しシンプルにもなります。
|
46
|
-
|
47
|
-
```HTML
|
48
|
-
|
49
|
-
<ul class="company">
|
50
|
-
|
51
|
-
<li><label><input class="input" type="checkbox" value="companyA" id="productA" name="company" onclick="dsProduct();" ><span>会社A</span></label></li>
|
52
|
-
|
53
|
-
<li><label><input class="input" type="checkbox" value="companyB" id="productB" name="company" onclick="dsProduct();" ><span>会社B</span></label></li>
|
54
|
-
|
55
|
-
<li><label><input class="input" type="checkbox" value="companyC" id="productC" name="company" onclick="dsProduct();" ><span>会社C</span></label></li>
|
56
|
-
|
57
|
-
</ul>
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
以下同じ
|
62
|
-
|
63
|
-
```
|
64
|
-
|
65
|
-
|
66
|
-
|
67
1
|
```JavaScript
|
68
2
|
|
69
3
|
function dsProduct() {
|
@@ -72,7 +6,9 @@
|
|
72
6
|
|
73
7
|
companies.forEach(function(company) {
|
74
8
|
|
9
|
+
var productClass = company.value.replace("company", "product");
|
10
|
+
|
75
|
-
var products = document.getElementsByClassName(c
|
11
|
+
var products = document.getElementsByClassName(productClass);
|
76
12
|
|
77
13
|
Array.prototype.forEach.call(products, function(product) {
|
78
14
|
|
@@ -84,4 +20,6 @@
|
|
84
20
|
|
85
21
|
}
|
86
22
|
|
23
|
+
|
24
|
+
|
87
25
|
```
|