我使用带有代码的文本字段:
<input type = "text" size = "2" maxlength = "2" name = "myname">
和一个带有以下内容的按钮:
<button type = "button" onclick = "alert('Clicked!')">Print</button>
现在,当我单击它时,会弹出一条消息/警告说Clicked!
。我怎样才能使其仅显示文本字段上的任何值?泰
试试这个解决方案:
<input type="text" id="test" size="2" maxlength="2" name="myname">
<button type="button" onclick="alert(document.getElementById('test').value);">Print</button>
您可以在这里找到一个工作示例:http : //jsfiddle.net/sebastianbrosch/x8zzvd5s/
解释
您必须在您的文本字段(此处test
)中添加一个id 。在上,alert()
您可以使用获取文本字段的值document.getElementById('test').value
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句