无法获取我的Javascript输出以填充HTML元素

时间:2015-06-30 00:23:45

标签: javascript html

在输出时遇到一些问题需要更改字段。 1.每次运行它都会变为空白然后重置。 2.字符串不会显示。

功能: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> 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在点击事件中:

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

您的按钮点击代码中的第二行应替换为上一行,这将更新文本区域。