2015-06-30 70 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; 

在你点击按钮的代码你的第二个行应与上述线路进行更换,这将更新文本区域。