Javascript获取input里的值

这里页面展示

JavaScript获取input里的值

以下是代码

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript获取input里的值title>
head>
<body>
    <input type="text" id="arr1">
    <input type="text" name="arr2" >
    <input type="text" class="arr3">
    <button onclick="save()">保存button>
    <script>
        function save(){
            var id = document.getElementById("arr1").value;
            console.log(id)
            var name = document.getElementsByName("arr2")[0].value;
            console.log(name)
            var classname = document.getElementsByClassName("arr3")[0].value;
            console.log(classname)
        }
    script>
body>
<style>
    .arr3{
        text-align: center;
    }
style>
html>

这里我们要注意一下:id用的是getElement,而name,class用的是getElements。这里多了一个s大家一定要注意哦,否则会出错的!

你可能感兴趣的:(JavaScript,javascript,html)