回答編集履歴
1
調整
answer
CHANGED
@@ -14,4 +14,46 @@
|
|
14
14
|
</select>
|
15
15
|
|
16
16
|
<input name="color_name" type="text" id="color_name" value="">
|
17
|
+
```
|
18
|
+
|
19
|
+
# 調整版
|
20
|
+
入力欄に100、200、300のどれかを入れるとselectが変化し、表示欄に値が表示される
|
21
|
+
```javascript
|
22
|
+
<script>
|
23
|
+
HTMLElement.prototype.trigger=function(eventStr){
|
24
|
+
if (document.createEvent) {
|
25
|
+
var e = document.createEvent("HTMLEvents");
|
26
|
+
e.initEvent(eventStr, true, true );
|
27
|
+
return this.dispatchEvent(e);
|
28
|
+
} else {
|
29
|
+
var e = document.createEventObject();
|
30
|
+
return this.fireEvent("on"+eventStr, e);
|
31
|
+
}
|
32
|
+
};
|
33
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
34
|
+
document.querySelector('#inp').addEventListener('keyup',function(e){
|
35
|
+
var v=e.target.value;
|
36
|
+
var c={"100":"red","200":"yellow","300":"blue"};
|
37
|
+
if(typeof c[v]!=="undefined"){
|
38
|
+
document.querySelector('#color').value=c[v];
|
39
|
+
}else {
|
40
|
+
document.querySelector('#color').value="";
|
41
|
+
}
|
42
|
+
document.querySelector('#color').trigger('change');
|
43
|
+
});
|
44
|
+
document.querySelector('#color').addEventListener('change',function(e){
|
45
|
+
var t=e.target;
|
46
|
+
document.querySelector('#color_name').value=t.selectedIndex==0?"":t.options[t.selectedIndex].textContent;
|
47
|
+
});
|
48
|
+
});
|
49
|
+
</script>
|
50
|
+
<select name="color" id="color">
|
51
|
+
<option value="">選択</option>
|
52
|
+
<option value="red">赤</option>
|
53
|
+
<option value="yellow">黄</option>
|
54
|
+
<option value="blue">青</option>
|
55
|
+
</select><br>
|
56
|
+
入力:<input type="text" id="inp" value=""><br>
|
57
|
+
表示:<input name="color_name" type="text" id="color_name" value="" readonly>
|
58
|
+
|
17
59
|
```
|