When I was learning javaweb, I did the exercise of adding the form information on the page to the database. When submitting a form, you need to ensure that each input box, radio button, check box, etc. is not empty. At the beginning, you get the values of the controls one by one to determine whether they are empty. Later, you learned about form serialization. Just use serialization to determine the null value.
//form form page
<form id="basicInfo" action="EmployeeServlet?flag=addEmployeeInfo" method="post"> <p class="formbody"> <p class="formtitle"><span>基本信息</span></p> <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> </p> </form>
Triggered when a submission is written in the external link js file The event
$('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; } } });
The above is the entire content of the example of JS form serialization to determine the null value brought by the editor. I hope you like it~
Related recommendations:
About PHP data serialization testing
Detailed explanation of PHP serialization and deserialization principles
jQuery form serialization example code example sharing
The above is the detailed content of js method to implement form serialization to determine null value. For more information, please follow other related articles on the PHP Chinese website!