[英]Trouble getting input value
I'm having trouble trying to log the value of a input.我在尝试记录输入值时遇到问题。
So my script is showing below.所以我的脚本显示在下面。
document.getElementById("news").innerHTML = '<input type="text" id="myText" value="Mickey">';
var x = document.getElementById("news").value;
console.log(x)
I expected it to log "Mickey" to the console.我希望它将“米奇”记录到控制台。 Instead it logs "undefined"相反,它记录“未定义”
Can someone help me understand why and possibly help me with a solution to my problem.有人可以帮助我理解为什么并可能帮助我解决我的问题。 Thank you.谢谢你。
You're trying to get the value from wrong element.您正试图从错误的元素中获取值。 Try using the input Id and you'll get results.尝试使用输入 ID,您将获得结果。
var x = document.getElementById("myText").value;
You need to get the value from input id--^您需要从输入 id 中获取值--^
document.getElementById("news").innerHTML = '<input type="text" id="myText" value="Mickey">'; var x = document.getElementById("myText").value; console.log(x)
<div id="news"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.