以下是对Javascript操作表单(验证)的学习
操作表单(验证)
表单是什么 form 本身也是DOM树
- 文本框 text
- 下拉框 select
- 单选框 radio
- 多选框 checkbox
- 隐藏域 hidden
- 密码框 password
- ….
获得要提交的信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <form action="post"> <p><span>用户名:</span> <input type="text" name="" 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'); input_text.value; boy_radio.checked; girl_radio.checked = true; </script>
|
提交表单。md5加密密码,表单优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
<form action="https://wwww.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 true; } </script>
|