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>