我正在努力实现以下目标,其中包括:
- 从 JavaScript 中的元素检索数据。
<p id='returnText'>Text</p>
- 将 from 的输入值设置为第 1 步中检索到的文本。
这就是我目前所拥有的,我真的被困住了:(
function autoMagicallySubmit(){
var html = document.getElementById("returnText").innerHTML;
html.replace(/<[^>]*>/g, "");
document.getElementById("sendText").value = html.text;
}
<p id='returnText'>TEXT NEEDED TO BE TAKEN!</p>
<form name="myForm"action="Contact" method="post">
<input type="text" name="sendText" id="sendText" value="">
</form>
但我不断在文本框中检索“未定义”。我有 php 回显步骤 1 下的元素。
最佳答案
document.getElementById("returnText").innerHTML;
返回文本,而不是具有“text”属性的对象。
你应该只有 document.getElementById("sendText").value = html
关于javascript - 从元素获取文本并将其作为值放入文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550208/