本文摘自PHP中文网,作者青灯夜游,侵删。
javascript可以获取input的值,方法:首先使用“document.getElementById("id值")”语句获取到input元素对象;然后使用“input元素对象.value”语句来获取input元素的值即可。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript可以获取input值
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< title >JavaScript中获取input元素value值</ title >
< link rel = "stylesheet" href = "" >
< script >
function printInputValue() {
var inputValue = document.getElementById("demo1").value;
console.log(inputValue);
document.getElementById("demo2").innerHTML = inputValue;
}
</ script >
</ head >
< body >
< input type = "text" id = "demo1" >
< button onclick = "printInputValue()" >获取input元素value值</ button >
< p id = "demo2" ></ p >
</ body >
</ html >
|
运行结果:

说明:
getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。
value 属性可设置或返回文本域的值。
【推荐学习:javascript高级教程】
以上就是javascript可以获取input的值吗的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
浅谈使用javascript如何进行ajax调用和请求
javascript中函数的调用和this的指向介绍(代码)
如何用javascript实现手势库
javascript中的强制类型转换的方法介绍
检测javascript里最有效率的功能特征的方法解析
javascript字符串转换成utf-8编码方式有哪些
javascript定义变量的方法有哪些
javascript阻塞问题
javascript声明变量的4种方法是什么
javascript中$是什么意思
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript可以获取input的值吗