javascript获取表单值的7种方式

时间:2022-05-24 11:04:07

见代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单对象--获取表单值的7种方式</title>
</head>
<body>
<form action="" name="myform">
<input type="text" name="user" value="admin">
</form> <script>
document.write(document.myform.user.value+"1<br/>");
document.write(document['myform'].user.value+"2<br/>");
document.write(document.forms.myform.user.value+"3<br/>");
document.write(document.forms[0].user.value+"5<br/>");
document.write(document.forms['myform'].user.value+"4<br/>");
document.write(document.forms.item(0).user.value+"6<br/>");
document.write(document.forms.item('myform').user.value+"7<br/>");//FF可能不支持 </script>
</body>
</html>

推荐使用

document.write(document.myform.user.value);