回答編集履歴
2
chousei
answer
CHANGED
@@ -14,7 +14,6 @@
|
|
14
14
|
s_b.disabled=s_a.selectedIndex==0;
|
15
15
|
fetch(url+'?select_a='+s_a.value).then(res=>res.json()).then(res=>{
|
16
16
|
const dom=res.map(x=>Object.assign(document.createElement('option'),{value:x.code,textContent:x.name}));
|
17
|
-
var count=0;
|
18
17
|
while(s_b.querySelectorAll('option').length>1){
|
19
18
|
s_b.removeChild(s_b.querySelector('option:nth-child(2)'));
|
20
19
|
}
|
1
sample
answer
CHANGED
@@ -1,3 +1,71 @@
|
|
1
1
|
サーバー側でSQLでも走らせているのでしょか?
|
2
2
|
であればselect_aのchangeイベントでajax(or fetch)でselect_bのリストを
|
3
|
-
もってくればいいでしょう
|
3
|
+
もってくればいいでしょう
|
4
|
+
|
5
|
+
- mypage.html
|
6
|
+
```javascript
|
7
|
+
<script>
|
8
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
9
|
+
const s_a=document.querySelector('#select_a');
|
10
|
+
const s_b=document.querySelector('#select_b');
|
11
|
+
const url="json.php";
|
12
|
+
s_b.disabled=true;
|
13
|
+
s_a.addEventListener('change',e=>{
|
14
|
+
s_b.disabled=s_a.selectedIndex==0;
|
15
|
+
fetch(url+'?select_a='+s_a.value).then(res=>res.json()).then(res=>{
|
16
|
+
const dom=res.map(x=>Object.assign(document.createElement('option'),{value:x.code,textContent:x.name}));
|
17
|
+
var count=0;
|
18
|
+
while(s_b.querySelectorAll('option').length>1){
|
19
|
+
s_b.removeChild(s_b.querySelector('option:nth-child(2)'));
|
20
|
+
}
|
21
|
+
dom.forEach(x=>s_b.appendChild(x));
|
22
|
+
});
|
23
|
+
});
|
24
|
+
});
|
25
|
+
</script>
|
26
|
+
<select id="select_a" name="select_a">
|
27
|
+
<option value="">= 選択 =</option>
|
28
|
+
<option value="0">東日本</option>
|
29
|
+
<option value="1">西日本</option>
|
30
|
+
<option value="2">海外</option>
|
31
|
+
<option value="3">ダミー</option> <!-- select_bは空になる -->
|
32
|
+
</select>
|
33
|
+
<select id="select_b"name="select_b">
|
34
|
+
<option value="">= 選択 =</option>
|
35
|
+
</select>
|
36
|
+
```
|
37
|
+
- json.php
|
38
|
+
```PHP
|
39
|
+
<?PHP
|
40
|
+
$select_a=filter_input(INPUT_GET,"select_a",FILTER_VALIDATE_INT);
|
41
|
+
$a=[
|
42
|
+
[
|
43
|
+
["code"=>"001","name"=>"営業A"],
|
44
|
+
["code"=>"002","name"=>"営業B"],
|
45
|
+
["code"=>"003","name"=>"営業C"],
|
46
|
+
["code"=>"004","name"=>"開発"],
|
47
|
+
],
|
48
|
+
[
|
49
|
+
["code"=>"001","name"=>"営業A"],
|
50
|
+
["code"=>"002","name"=>"営業B"],
|
51
|
+
["code"=>"003","name"=>"開発"]
|
52
|
+
],
|
53
|
+
[
|
54
|
+
["code"=>"001","name"=>"営業A"],
|
55
|
+
["code"=>"002","name"=>"営業B"],
|
56
|
+
["code"=>"003","name"=>"開発A"],
|
57
|
+
["code"=>"004","name"=>"開発B"],
|
58
|
+
],
|
59
|
+
];
|
60
|
+
$list_a=array_keys($a);
|
61
|
+
if(in_array($select_a,$list_a,true)===false){
|
62
|
+
$select_a=null;
|
63
|
+
}
|
64
|
+
|
65
|
+
if(!is_null($select_a)){
|
66
|
+
print json_encode($a[$select_a]);
|
67
|
+
}else{
|
68
|
+
print "[]";
|
69
|
+
}
|
70
|
+
?>
|
71
|
+
```
|