2017-03-01 83 views
1

我有一個國家的名單與他們的呼號,我希望通過選擇一個國家在列表中的國家的指示對應於我的輸入,但它不工作...我做找不到解決插入一個值到輸入

<script type="text/javascript"> 
var mytextbox = document.getElementById('Recipient'); 
var mydropdown = document.getElementById('indicatif'); 

mydropdown.onchange = function(){ 
    mytextbox.innerHTML = this.value; 
} 
</script> 
<select style="visibility:hidden;" style="color:black;" id="indicatif"> 
    <option value="">Destination country</option> 
    <option value="+355">Albania</option> 
    <option value="+213">Algeria</option> 
    <option value="+376">Andorra</option> 
    <option value="+244">Angola</option> 
    <option value="+1264">Anguilla</option> 
    <option value="+1268">Antigua and Barbuda</option> 
    <option value="+54">Argentina</option> 
    <option value="+374">Armenia</option> 
    <option value="+297">Aruba</option> 
    <option value="+61">Australia</option> 
    <option value="+43">Austria</option> 
    <option value="+994">Azerbaijan</option> 
    <option value="+1242">Bahamas</option> 
    .... 
</select> 
<input style="color:black;" name="Recipient" id="Recipient" type="text"> 
+0

''元素有'.value'不'.innerHTML' –

回答

4

var mytextbox = document.getElementById('Recipient'); 
 
var mydropdown = document.getElementById('indicatif'); 
 

 
mydropdown.addEventListener('change', function(){ 
 
    mytextbox.value = mydropdown.value; 
 
});
<select style="color:black;" id="indicatif"> 
 
    <option value="">Destination country</option> 
 
    <option value="+355">Albania</option> 
 
    <option value="+213">Algeria</option> 
 
    <option value="+376">Andorra</option> 
 
    <option value="+244">Angola</option> 
 
    <option value="+1264">Anguilla</option> 
 
    <option value="+1268">Antigua and Barbuda</option> 
 
    <option value="+54">Argentina</option> 
 
    <option value="+374">Armenia</option> 
 
    <option value="+297">Aruba</option> 
 
    <option value="+61">Australia</option> 
 
    <option value="+43">Austria</option> 
 
    <option value="+994">Azerbaijan</option> 
 
    <option value="+1242">Bahamas</option> 
 
    .... 
 
</select> 
 
<input style="color:black;" name="Recipient" id="Recipient" type="text">

完整的代碼片段,只需複製並粘貼到您的項目。

<html> 
 

 
<head> 
 
    <title>HelloWorld</title> 
 

 
</head> 
 

 
<body> 
 

 
    <select style="color:black;" id="indicatif"> 
 
     <option value="">Destination country</option> 
 
     <option value="+355">Albania</option> 
 
     <option value="+213">Algeria</option> 
 
     <option value="+376">Andorra</option> 
 
     <option value="+244">Angola</option> 
 
     <option value="+1264">Anguilla</option> 
 
     <option value="+1268">Antigua and Barbuda</option> 
 
     <option value="+54">Argentina</option> 
 
     <option value="+374">Armenia</option> 
 
     <option value="+297">Aruba</option> 
 
     <option value="+61">Australia</option> 
 
     <option value="+43">Austria</option> 
 
     <option value="+994">Azerbaijan</option> 
 
     <option value="+1242">Bahamas</option> 
 
    </select> 
 
    <input style="color:black;" name="Recipient" id="Recipient" type="text"> 
 

 
    <script> 
 
    var mytextbox = document.getElementById('Recipient'); 
 
    var mydropdown = document.getElementById('indicatif'); 
 

 
    mydropdown.addEventListener('change', function() { 
 
     mytextbox.value = mydropdown.value; 
 
    }); 
 
    </script> 
 
</body> 
 

 
</html>

+0

我不明白爲什麼你的代碼不能在我的網站工作:/ – Chrys

+0

我在本地主機上嘗試過,它不起作用o_O – Chrys

+1

非常感謝你,它非常好用 – Chrys

1

試試這個:

mydropdown.onchange = function(){ 
    mytextbox.value = mydropdown.value; 
} 
+0

你的腳本和「善良的用戶」腳本工作在https://jsfiddle.net/但在本地主機和我的網站,它不起作用,我不明白 – Chrys