Js获取标签中的值
2017-08-11 本文已影响0人
lx_smile
在HTML中有如下代码,编写代码获取邮箱的值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form id = "myform" name = "myform" action = "">
邮箱:<input type = "text" id = "email" name = "email">
<button type = "submit" onclick = "test()">提交</button>
</form>
</body>
</html>
- 方法1
function test(){
var form = document.getElementById("email").value;
alert(form);
}
- 方法2
function test(){
var form = document.forms["myform"]["email"].value;
alert(form);
}
- 方法3
function test(){
var form = document.myform.elements[0].value;
alert(form);
}
- 方法4
function test(){
var form = document.getElementsByName("email")[0].value;
alert(form);
}
- 效果展示