回答編集履歴

1

調整

2018/11/15 10:56

投稿

yambejp
yambejp

スコア114915

test CHANGED
@@ -31,3 +31,87 @@
31
31
  <input name="color_name" type="text" id="color_name" value="">
32
32
 
33
33
  ```
34
+
35
+
36
+
37
+ # 調整版
38
+
39
+ 入力欄に100、200、300のどれかを入れるとselectが変化し、表示欄に値が表示される
40
+
41
+ ```javascript
42
+
43
+ <script>
44
+
45
+ HTMLElement.prototype.trigger=function(eventStr){
46
+
47
+ if (document.createEvent) {
48
+
49
+ var e = document.createEvent("HTMLEvents");
50
+
51
+ e.initEvent(eventStr, true, true );
52
+
53
+ return this.dispatchEvent(e);
54
+
55
+ } else {
56
+
57
+ var e = document.createEventObject();
58
+
59
+ return this.fireEvent("on"+eventStr, e);
60
+
61
+ }
62
+
63
+ };
64
+
65
+ window.addEventListener('DOMContentLoaded', function(e){
66
+
67
+ document.querySelector('#inp').addEventListener('keyup',function(e){
68
+
69
+ var v=e.target.value;
70
+
71
+ var c={"100":"red","200":"yellow","300":"blue"};
72
+
73
+ if(typeof c[v]!=="undefined"){
74
+
75
+ document.querySelector('#color').value=c[v];
76
+
77
+ }else {
78
+
79
+ document.querySelector('#color').value="";
80
+
81
+ }
82
+
83
+ document.querySelector('#color').trigger('change');
84
+
85
+ });
86
+
87
+ document.querySelector('#color').addEventListener('change',function(e){
88
+
89
+ var t=e.target;
90
+
91
+ document.querySelector('#color_name').value=t.selectedIndex==0?"":t.options[t.selectedIndex].textContent;
92
+
93
+ });
94
+
95
+ });
96
+
97
+ </script>
98
+
99
+ <select name="color" id="color">
100
+
101
+ <option value="">選択</option>
102
+
103
+ <option value="red">赤</option>
104
+
105
+ <option value="yellow">黄</option>
106
+
107
+ <option value="blue">青</option>
108
+
109
+ </select><br>
110
+
111
+ 入力:<input type="text" id="inp" value=""><br>
112
+
113
+ 表示:<input name="color_name" type="text" id="color_name" value="" readonly>
114
+
115
+
116
+
117
+ ```