繁体   English   中英

为什么我无法获取此文本输入字段的值?

[英]Why can't I get the value of this text input field?

我有一个 div,其中包含一个文本输入字段:

<div id="age" class="fullScreen">
  <input type="text" id="age" class="textInput" placeholder="Please enter age..." />
  <button type="button" onclick="submitAge()">Submit</button>
</div>

submitAge() 看起来像这样:

function submitAge() {
  var ageVal = document.getElementById("age").text;
  alert(ageVal);
}

但是在 Google Chrome 和 Webkit Nightly 中,我看到一个带有“未定义”文本的警报(我无法在任何其他浏览器中测试,因为该页面包含网络套接字)。

chrome 开发人员工具显示了这一点:

年龄值:未定义

我认为问题在于输入不在表单中,但我不希望它在表单中,因为我将通过 websockets 提交数据。

我必须把它放在一个表格中还是有更好的解决方案?

谢谢。

您已经重新定义了 ID“年龄”。 重命名您的 div 或输入,您应该没问题。

尝试:

function submitAge() {
  var ageVal = document.getElementById("age").value;
  alert(ageVal);
}
var ageVal = document.getElementById('age').value;

alert(ageVal)

注意 .value 而不是 text

改变:

 var ageVal = document.getElementById("age").text;

 var ageVal = document.getElementById("age").value;

尝试

document.getDocumentById("age").value;

你的<div>有年龄的 id。 这将返回 div 或 HTMLElements 数组。 获取输入的唯一id,您将是金子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM