表单是什么?form-----DOM树
表单的目的:提交信息
我们获得要提交的信息
写个表单
1)得到输入框的值
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <form action="post">
- <span>用户名:span><input type="text"id="username">
- form>
- <script>
- var input_text = document.getElementById('username');
-
- script>
- body>
- html>

2)修改输入框的值

3)对于单选框和多选框,查看返回结果:
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <form action="post">
- <p>
- <span>用户名:span><input type="text"id="username">
- p>
- <p>
- <span>性别:span>
- <input type="radio" name="sex" value="man" id="boy">男
- <input type="radio" name="sex" value="woman" id="girl">女
- p>
-
- form>
- <script>
- var input_text = document.getElementById('username');
- var boy_radio = document.getElementById('boy');
- var girl_radio = document.getElementById('girl');
-
- //对于单选框,多选框等固定的值,用value只能取到当前的值
-
- script>
- body>
- html>

简单测试表单的提交:
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <form action="post">
- <p>
- <span>用户名:span><input type="text" id="username">
- p>
- <p>
- <span>密码:span><input type="password" id="password">
- p>
- <button type="button" onclick="aaa()">提交button>
- form>
- <script>
- function aaa() {
- var uname = document.getElementById('username');
- var pwd = document.getElementById('password');
- console.log(uname.value);
- console.log(pwd.value);
- }
- script>
- body>
- html>

md5为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。md5将整个文件当作一个大文本信息,通过其不可逆的字符串变换算法,产生了这个唯一的md5信息摘要。
MD5加密工具类:https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
-
- <script src = "https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js">
-
- script>
- head>
- <body>
- <form action="#" method="post">
- <p>
- <span>用户名:span><input type="text" id="username">
- p>
- <p>
- <span>密码:span><input type="password" id="password">
- p>
- <button type="button" onclick="aaa()">提交button>
- form>
- <script>
- function aaa() {
- var uname = document.getElementById('username');
- var pwd = document.getElementById('password');
- console.log(uname.value);
- //MD5加密
- pwd.value=md5(pwd);
- console.log(pwd.value);
- }
- script>
- body>
- html>

按钮绑定提交事件:
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
-
- <script src = "https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js">
-
- script>
- head>
- <body>
- <form action="#" method="post">
- <p>
- <span>用户名:span><input type="text" id="username" name="username">
- p>
- <p>
- <span>密码:span><input type="password" id="input-password">
- p>
- <input type="hidden" id="md5-password" name="password">
- <button type="submit" onclick="aaa()">提交button>
- form>
- <script>
- function aaa() {
- var uname = document.getElementById('username');
- var pwd = document.getElementById('input-password');
- var md5pwd = document.getElementById('md5-password');
-
- md5pwd.value=md5(pwd.value);
- return false;
- }
- script>
- body>
- html>
表单绑定提交事件
需要通过οnsubmit= 绑定一个提交检测的函数 :true false
再将这个结果返回给表单,使用onsubmit接收
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
-
- <script src = "https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js">
-
- script>
- head>
- <body>
- <form action="https://www.baidu.com/" method="post" onsubmit="return aaa()">
- <p>
- <span>用户名:span><input type="text" id="username" name="username">
- p>
- <p>
- <span>密码:span><input type="password" id="input-password">
- p>
- <input type="hidden" id="md5-password" name="password">
- <button type="submit">提交button>
- form>
- <script>
- function aaa() {
- alert(1);
- var uname = document.getElementById('username');
- var pwd = document.getElementById('input-password');
- var md5pwd = document.getElementById('md5-password');
-
- md5pwd.value=md5(pwd.value);
- return false;
- }
- script>
- body>
- html>