前端提交表单两种验证方式记录 jq或h5 required属性
程序员文章站
2022-04-10 14:36:09
JQuery: HTML5 required属性: ......
jquery:
<form id="form"> <input type="text" name="aaa"> <select name="bbb"> <option value="">请选择</option> <option value="1">选项1</option> <option value="2">选项2</option> <option value="3">选项3</option> </select> <button type="button" id="submit">提交</button> </form>
//提交表单 $('#submit').click(function(){ var data = {}; var t = $('#form').serializearray(); $.each(t, function() { data [this.name] = this.value; }); console.log(data); if(data.aaa == ''){alert("aaa不能为空!");return false;} if(data.bbb == ''){alert("bbb不能为空!");return false;} $('#form').submit(); });
html5 required属性:
<input type="text" name="aaa" required="required"> <select name="bbb" required="required"> <option value="">请选择</option> <option value="1">选项1</option> <option value="2">选项2</option> <option value="3">选项3</option> </select> <button type="submit">提交</button>
上一篇: ios开发网络篇—Get请求和Post请求 - 转
下一篇: 剑指offer打卡-1