js表单序列化判断空值的实例
作者:Mr-Cuba 时间:2024-04-22 13:01:15
学习javaweb的时候,做了将页面上的的表单信息添加到数据库中的练习。提交表单的时候,需要保证每个输入框、单选按钮、复选框等都不为空,刚开始的时候挨个获取控件的值进行判断是否为空,后来认识了表单序列化这么一个方法,就是了下用序列化来判断空值
//form表单页面
<form id="basicInfo" action="EmployeeServlet?flag=addEmployeeInfo" method="post">
<div class="formbody">
<div class="formtitle"><span>基本信息</span></div>
<ul class="forminfo">
<li>
<label>员工账号</label>
<input id="account" name="account" type="text" class="dfinput" readOnly="true" />
<i>设置员工姓名和入职时间后,系统自动生成,无法更改</i>
</li>
<li>
<label>员工姓名</label>
<input name="name" type="text" class="dfinput" id="name" />
<i>姓名不能为数字</i>
</li>
<li>
<label>密码</label>
<input name="password" type="password" class="dfinput" id="pwd" placeholder="空值或空白则默认密码为123456" />
<i></i>
</li>
<li>
<label>性别</label>
<cite>
<input name="gender" type="radio" value="男" checked="checked" />男
<input name="gender" type="radio" value="女" />女
</cite>
</li>
<li>
<label>生日</label>
<input id="birth" name="birthday" type="text" class="dfinput" placeholder="格式:yyyy-mm-dd"/>
<i id = "birthTip"></i>
</li>
<li>
<label>手机号码</label>
<input id="phone" name="telephone" type="text" class="dfinput" />
<i id = "phoneTip"></i>
</li>
<li>
<label>邮箱</label>
<input id="email" name="email" type="text" class="dfinput" />
<i id = "emailTip"></i>
</li>
<li>
<label>地址</label>
<input name="address" type="text" class="dfinput" />
<i id = "addressTip"></i>
</li>
<li>
<label>所属部门</label>
<select id="dept" class="dfinput" name="dept">
</select>
</li>
<li>
<label>入职时间</label>
<input id="entrytime" name="entrytime" type="text" class="dfinput" placeholder="格式:yyyy-mm-dd" />
<i id = "timeTip"></i>
</li>
<li>
<label>员工状态</label>
<select id="empstate" class="dfinput" name="empstate">
</select>
</li>
<li>
<label>直属上级</label>
<input id="superior" name="superior" type="text" class="dfinput" placeholder="直属上级员工id" />
<i id = "superTip"></i>
</li>
<li>
<label>职务</label>
<select id="dutyid" class="dfinput" name="dutyid">
</select>
</li>
<li>
<label>角色权限</label>
<select id="authority" class="dfinput" name="authority">
</select>
</li>
<li>
<label> </label>
<input id="savebtn" type="submit" class="btn" value="确认保存"/>
</li>
</ul>
</div>
</form>
在外链js文件中写了个提交时触发的事件
$('form').submit(function(){
//当密码为空的时候自动赋值
var pwd = $('#pwd').val();
if(pwd==null || pwd == ''){
$('#pwd').val('123456');
}
var data = $('form').serialize();
//表单序列化后返回一个字符串 如:account=123&password=1234&sex=&dept=2
var array = data.split('&');
//把字符串按&号分隔成数组 得到 {account=123,password=1234,sex=,dept=2} 字符串数组
for(var i = 0;i < array.length; i++){
var kwarr = array[i].split('=');
//循环将数组中的每个子元素字符串用=号分隔成数组 {account,123} {sex,} 然后判断索引为1的子元素是否存在或为‘' 从而达到了表单判空的目的
if(kwarr[1]===null || kwarr[1] ===''){
alert('除密码外不能存在空值');
return false;
}
}
});
如果大家想了解更多游戏活动与游戏攻略,请持续关注本站,本站小编将在第一时间为大家带来最好看、最好玩、最新鲜的游戏资讯。更多精彩内容,尽在jb51游戏频道!
来源:http://www.cnblogs.com/Cuba236/archive/2017/09/22/7573056.html
标签:js,表单序列化,空值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
首页访问感受提升三步曲
2007-12-13 20:36:00
![](https://img.aspxhome.com/file/UploadPic/up/2007121321122194.gif)
matplotlib.pyplot.matshow 矩阵可视化实例
2022-01-04 02:57:21
![](https://img.aspxhome.com/file/2023/6/92006_0s.jpg)
解决Win7 x64安装解压版mysql 5.7.18 winx64出现服务无法启动问题
2024-01-22 07:57:39
![](https://img.aspxhome.com/file/2023/5/117835_0s.png)
Python程序设计入门(1)基本语法简介
2023-09-12 05:03:31
Python+PyQt5制作一个图片查看器
2021-03-03 04:16:06
![](https://img.aspxhome.com/file/2023/5/87995_0s.gif)
不同分辨率下的自适用宽度
2008-11-24 12:58:00
![](https://img.aspxhome.com/file/UploadPic/200811/24/2008112413340367s.jpg)
mysql中explain用法详解
2024-01-13 16:28:25
实现PHP框架系列文章(6)mysql数据库方法
2024-05-02 17:08:04
利用Python实现原创工具的Logo与Help
2021-06-03 20:43:05
![](https://img.aspxhome.com/file/2023/5/131975_0s.jpg)
Python中的flask框架详解
2021-03-20 20:23:59
MySQL中如何清空表数据
2024-01-16 19:57:28
解析MySQL索引的作用
2024-01-20 09:51:03
![](https://img.aspxhome.com/file/2023/5/82255_0s.png)
MYSQL主从数据库同步备份配置的方法
2024-01-23 15:03:43
Laravel实现ApiToken认证请求
2023-11-24 10:35:48
![](https://img.aspxhome.com/file/2023/7/92817_0s.jpg)
TensorFlow损失函数专题详解
2023-08-17 10:12:13
![](https://img.aspxhome.com/file/2023/3/85303_0s.gif)
SQL Server"错误 21002: [SQL-DMO]用户 * 已经存在问题解决
2024-01-16 16:01:55
用sysbench来测试MySQL的性能的教程
2024-01-16 10:56:52
Django2.1.7 查询数据返回json格式的实现
2023-09-28 09:54:20
![](https://img.aspxhome.com/file/2023/9/63239_0s.png)
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2023-11-12 04:37:43
![](https://img.aspxhome.com/file/2023/2/63182_0s.jpg)
Python中强大的命令行库click入门教程
2023-05-01 04:24:09