JavaScript可以获取input值

代码示例:

<!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>

运行结果:

javascript可以获取input的值吗

说明:

getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。

value 属性可设置或返回文本域的值。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。