gpt4 book ai didi

javascript - 无法让我的 Javascript 输出来填充 HTML 元素

转载 作者:行者123 更新时间:2023-12-03 09:43:07 25 4
gpt4 key购买 nike

在获取输出以更改字段时遇到一些问题。 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>

最佳答案

在点击事件中:

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

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

关于javascript - 无法让我的 Javascript 输出来填充 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31127634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com