JavaScript获取input元素的值

<html>
<head>
<script language="javascript">
  function print(){
    //var name = document.getElementById("username").value;//方法1
    var name = userForm.username.value; //方法2
    alert(name);
  }
 </script>
</head>
<body>
    <form name="userForm">
        <input type="text" id="username" /> 
        <input type="button" value="获取input值" onclick="print()" />
    </form>
</body>
</html>

方法一:使用元素ID

document.getElementById("username").value;

username是input的name值

方法二: 使用表单ID

userForm.username.value;

标签: JavaScript

相关文章推荐: