2016.06.17廖雪峰JS__学习笔记(操作表单)__P13
来源:互联网 发布:手机打牌赚钱软件 编辑:程序博客网 时间:2024/06/05 08:21
HTML表单的输入控件主要有以下几种:
文本框,对应的
<input type="text">
,用于输入文本;口令框,对应的
<input type="password">
,用于输入口令;单选框,对应的
<input type="radio">
,用于选择一项;复选框,对应的
<input type="checkbox">
,用于选择多项;下拉框,对应的
<select>
,用于选择一项;隐藏文本,对应的
<input type="hidden">
,用户不可见,但表单提交时会把隐藏文本发送到服务器。
获取值
如果我们获得了一个<input>
节点的引用,就可以直接调用value
获得对应的用户输入值:
// <input type="text" id="email">var input = document.getElementById('email');input.value; // '用户输入的值'
这种方式可以应用于text
、password
、hidden
以及select
。但是,对于单选框和复选框,value
属性返回的永远是HTML预设的值,而我们需要获得的实际是用户是否“勾上了”选项,所以应该用checked
判断:
// <label><input type="radio" name="weekday" id="monday" value="1"> Monday</label>// <label><input type="radio" name="weekday" id="tuesday" value="2"> Tuesday</label>var mon = document.getElementById('monday');var tue = document.getElementById('tuesday');mon.value; // '1'tue.value; // '2'mon.checked; // true或者falsetue.checked; // true或者false
提交表单
方式一是通过<form>
元素的submit()
方法提交一个表单,例如,响应一个<button>
的click
事件,在JavaScript代码中提交表单:
<!-- HTML --><form id="test-form"> <input type="text" name="test"> <button type="button" onclick="doSubmitForm()">Submit</button></form><script>function doSubmitForm() { var form = document.getElementById('test-form'); // 可以在此修改form的input... // 提交form: form.submit();}</script>
这种方式的缺点是扰乱了浏览器对form的正常提交。浏览器默认点击<button type="submit">
时提交表单,或者用户在最后一个输入框按回车键。
因此,第二种方式是响应<form>
本身的onsubmit
事件,在提交form时作修改:
<!-- HTML --><form id="test-form" onsubmit="return checkForm()"> <input type="text" name="test"> <button type="submit">Submit</button></form><script>function checkForm() { var form = document.getElementById('test-form'); // 可以在此修改form的input... // 继续下一步: return true;}</script>
注意要return true
来告诉浏览器继续提交,如果return false
,浏览器将不会继续提交form,这种情况通常对应用户输入有误,提示用户错误信息后终止提交form。
在检查和修改<input>
时,要充分利用<input type="hidden">
来传递数据。
例如,很多登录表单希望用户输入用户名和口令,但是,安全考虑,提交表单时不传输明文口令,而是口令的MD5。普通JavaScript开发人员会直接修改<input>
:
<!-- HTML --><form id="login-form" method="post" onsubmit="return checkForm()"> <input type="text" id="username" name="username"> <input type="password" id="password" name="password"> <button type="submit">Submit</button></form><script>function checkForm() { var pwd = document.getElementById('password'); // 把用户输入的明文变为MD5: pwd.value = toMD5(pwd.value); // 继续下一步: return true;}</script>
这个做法看上去没啥问题,但用户输入了口令提交时,口令框的显示会突然从几个*
变成32个*
(因为MD5有32个字符)。
要想不改变用户的输入,可以利用<input type="hidden">
实现:
<!-- HTML --><form id="login-form" method="post" onsubmit="return checkForm()"> <input type="text" id="username" name="username"> <input type="password" id="input-password"> <input type="hidden" id="md5-password" name="password"> <button type="submit">Submit</button></form><script>function checkForm() { var input_pwd = document.getElementById('input-password'); var md5_pwd = document.getElementById('md5-password'); // 把用户输入的明文变为MD5: md5_pwd.value = toMD5(input_pwd.value); // 继续下一步: return true;}</script>
注意到id
为md5-password
的<input>
标记了name="password"
,而用户输入的id
为input-password
的<input>
没有name
属性。没有name
属性的<input>
的数据不会被提交。
- 2016.06.17廖雪峰JS__学习笔记(操作表单)__P13
- 2016.06.17廖雪峰JS__学习笔记(操作DOM)__P12
- 2016.06.15廖雪峰JS__学习笔记(JSON)__P7
- 2016.06.19廖雪峰JS__学习笔记(Promise)__P14
- 2016.06.13廖雪峰JS__学习笔记(快速入门部分)__p1
- 2016.06.13廖雪峰JS__学习笔记(函数部分)__p2
- 2016.06.14廖雪峰JS__学习笔记(高阶函数)__P3
- 2016.06.14廖雪峰JS__学习笔记(函数部分)__P4
- 2016.06.14廖雪峰JS__学习笔记(对象部分)__P5
- 2016.06.15廖雪峰JS__学习笔记(正则表达式)__P6
- 2016.06.15廖雪峰JS__学习笔记(原型对象)__P8
- 2016.06.15廖雪峰JS__学习笔记(原型继承)__P9
- 2016.06.15廖雪峰JS__学习笔记(class继承)__P10
- 2016.06.15廖雪峰JS__学习笔记(浏览器对象)__P11
- 廖雪峰python学习笔记13.操作文件和目录
- 廖雪峰GIT学习笔记
- 廖雪峰Python学习笔记
- 廖雪峰python学习笔记
- CentOS7基本命令汇总
- 发现Java StringBuffer使用中的一个隐藏问题,你中招了吗?
- Intent和IntentFilter详解
- 单例模式
- 实例化讲解 RunLoop
- 2016.06.17廖雪峰JS__学习笔记(操作表单)__P13
- 数组中大于等于所有元素平均值的元素
- plc从入门到精通(一)低压电器的基本原理
- handler.obtainMessage()与Message.obtain()的区别
- EMACS学习笔记
- Glide入门简介
- RunTime 应用实例–关于埋点的思考
- 模式识别(Pattern Recognition)学习笔记(三十一)-- 线性回归
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之四:JdbcTemplate的事务管理