javascript - How would I select an input from a form and store it in a variable?
问题描述
For example, if i had a form like this:
<form id="form1">
<input type="text" name="item">
</form>
<button onclick="outputItem"></button>
How would I output the input from the form?
解决方案
您可以编写一个函数,您可以在其中使用querySelector()
获取元素来获取输入的值:
function outputItem(){
var v = document.querySelector('#form1 > input[name=item]').value;
console.log(v);
}
<form id="form1">
<input type="text" name="item">
</form>
<button onclick="outputItem()">Print</button>
推荐阅读
- android - windowmanager 没有收到某些具有相同操作系统版本的设备的点击?
- javascript - 如何将文件从 React 应用程序上传到 azure 商店模拟器
- angular - 每个项目的列表视图中的角度多个本地引用
- r - 错误:-没有适用于“公式”类对象的“svm”方法-
- php - 在本地 Mac 上编译 php-solr 失败
- javascript - jquery在树中查找下一个/上一个
- google-sheets - 谷歌电子表格:无需身份证明的不同权利
- objective-c - Objc 运行时设置 ivar 的值
- python-3.x - 如何修复此用户名和密码?
- git - 尝试发出拉取请求的登录错误