2015-06-30 95 views
0

有一些問題讓我的輸出更改字段。 1.每次運行它都會變成空白,然後重置。 2.字符串不會顯示。無法讓我的Javascript輸出填充HTML元素

功能:HTML通過JS輸入字符串,然後在按鈕點擊發生時輸出到頁面上的HTML元素。

var button = document.getElementById('test'); 
 
    var date = document.getElementById('1'); 
 
    var contact = document.getElementById('2'); 
 
    var contacttype = document.getElementById('3'); 
 
    var os = document.getElementById('4'); 
 
    var devicetype = document.getElementById('5'); 
 
    var device = document.getElementById('6'); 
 
    var reason = document.getElementById('7'); 
 
    var comments = document.getElementById('8'); 
 
    var myTextArea = document.getElementById('myTextarea'); 
 

 

 
    button.onclick = function() { 
 
     var str = "Date: " + date.value + " " + "Contact: " + contact.value + " " + "Insured or Agent: " + contacttype.value + " " + "Operating System: " + os.value + " " + "Tablet or Phone: " + devicetype.value + " " + "Device Name: " + device.value + " " + "Reason fo Call: " + reason.value + " " + "Additional Comments: " + comments.value; 
 
     var myTextArea = document.getElementById('myTextArea'); 
 

 

 
    };
<body> 
 
    <p>Enter the information then hit comment, it will display a comment that can be <b>Copy+Pasted</b> into your SR. 
 
    </p> 
 

 
    <label>Date: 
 
     <input id="1" /> 
 
    </label> 
 
    <br /> 
 
    <label>Contact: 
 
     <input id="2" /> 
 
    </label> 
 
    <br> 
 
    <label>Insured or Agent: 
 
     <input id="3" /> 
 
    </label> 
 
    <br> 
 
    <label>Operating System: 
 
     <input id="4" /> 
 
    </label> 
 
    <br> 
 
    <label>Tablet or Phone: 
 
     <input id="5" /> 
 
    </label> 
 
     <br> 
 
     <label>Device Name: 
 
     <input id="6" /> 
 
    </label> 
 
      <br> 
 
     <label>Reason for call: 
 
     <input id="7" /> 
 
    </label> 
 
       <br> 
 
     <label>Additional Comments: 
 
     <input id="8" /> 
 
    </label> 
 

 

 
    <br /> 
 
    <button id="test">Display Comment</button> 
 
    <br> 
 
    <textarea id="myTextarea" name="something">This text gets removed</textarea> 
 

 

 

 

 
    </body>

回答

1

裏面您的單擊事件:

document.getElementById('myTextarea').value = str; 

在你點擊按鈕的代碼你的第二個行應與上述線路進行更換,這將更新文本區域。