回答編集履歴

1

加筆

2017/07/25 02:25

投稿

退会済みユーザー
test CHANGED
@@ -1,4 +1,4 @@
1
- 以下のselectを取得している部分を動的にすれば、コードの一本化ができます。
1
+ 以下のselectを取得している部分を
2
2
 
3
3
  ``` javascript
4
4
 
@@ -15,6 +15,58 @@
15
15
  var select1 = document.forms.formName.selectName3;
16
16
 
17
17
  var select2 = document.forms.formName.selectName4;
18
+
19
+ ```
20
+
21
+
22
+
23
+ 以下のように動的にすれば、コードの一本化ができます。
24
+
25
+ 合わせてHTMLも修正してください。
26
+
27
+ ``` javascript
28
+
29
+ function select(_parentSelect, _childSelect)
30
+
31
+ {
32
+
33
+ var parentSelect = typeof _parentSelect === 'string' ? document.forms.formName[_parentSelect] : _parentSelect;
34
+
35
+ var childSelect = document.forms.formName[_childSelect];
36
+
37
+
38
+
39
+ childSelect.options.length = 0;
40
+
41
+
42
+
43
+ if (parentSelect.options[parentSelect.selectedIndex].value == "TV")
44
+
45
+ {
46
+
47
+ childSelect.options[0] = new Option("SONY");
48
+
49
+ childSelect.options[1] = new Option("PANA");
50
+
51
+ childSelect.options[2] = new Option("TOSHI");
52
+
53
+ }
54
+
55
+
56
+
57
+ else if (parentSelect.options[parentSelect.selectedIndex].value == "PHONE")
58
+
59
+ {
60
+
61
+ childSelect.options[0] = new Option("APPLE");
62
+
63
+ childSelect.options[1] = new Option("GOOGLE");
64
+
65
+ childSelect.options[2] = new Option("ANDOROID");
66
+
67
+ }
68
+
69
+ }
18
70
 
19
71
  ```
20
72
 
@@ -64,54 +116,6 @@
64
116
 
65
117
  </body>
66
118
 
67
-
68
-
69
119
  ```
70
120
 
71
121
 
72
-
73
- ``` javascript
74
-
75
- function select(_parentSelect, _childSelect)
76
-
77
- {
78
-
79
- var parentSelect = typeof _parentSelect === 'string' ? document.forms.formName[_parentSelect] : _parentSelect;
80
-
81
- var childSelect = document.forms.formName[_childSelect];
82
-
83
-
84
-
85
- childSelect.options.length = 0;
86
-
87
-
88
-
89
- if (parentSelect.options[parentSelect.selectedIndex].value == "TV")
90
-
91
- {
92
-
93
- childSelect.options[0] = new Option("SONY");
94
-
95
- childSelect.options[1] = new Option("PANA");
96
-
97
- childSelect.options[2] = new Option("TOSHI");
98
-
99
- }
100
-
101
-
102
-
103
- else if (parentSelect.options[parentSelect.selectedIndex].value == "PHONE")
104
-
105
- {
106
-
107
- childSelect.options[0] = new Option("APPLE");
108
-
109
- childSelect.options[1] = new Option("GOOGLE");
110
-
111
- childSelect.options[2] = new Option("ANDOROID");
112
-
113
- }
114
-
115
- }
116
-
117
- ```