表单提交方式一,按钮onclick绑定
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><!-- MD5 工具类 --><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="password" name="password"></p><!-- <input type="submit">--><!-- 绑定事件 onclick 被点击 -->
<!-- <button type="submit" οnclick="aaa()">提交</button>--><button type="submit" onclick="aaa()">提交</button>
</form>
<script>function aaa() {let uname = document.getElementById('username')let pwd = document.getElementById('password')console.log(uname.value)// MD5 算法pwd.value = md5(pwd.value)console.log(pwd.value)}
</script>
</body>
</html>
这种方式有个坏处,点击提交的一瞬间能看到密码框中的密码变长了
表单提交方式二,表单验证 onsubmit
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><!-- MD5 工具类 --><script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
</head>
<body>
<form action="#" 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">
<!-- <input type="submit">--><!-- 绑定事件 onclick 被点击 -->
<!-- <button type="submit" οnclick="aaa()">提交</button>--><button type="submit">提交</button>
</form>
<script>function aaa() {let uname = document.getElementById('username')let pwd = document.getElementById('input-password')let md5pwd = document.getElementById('md5-password')md5pwd.value = md5(pwd.value)// 可以检验判断表单内容,true就是通过提交,false,阻止提交return true;}
</script>
</body>
</html>
https://www.bilibili.com/video/BV1JJ41177di?p=25