1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <form action="" id="Form"> 9 用户名:<input type="text" name="username" class="con" mark="用户名"><br> 10 密码 :<input type="password" name="pwd" class="con" mark="密码"><br> 11 <input type="submit" value="submit"> 12 13 14 </form> 15 16 <script src="jquery-3.2.1.js"></script> 17 <script> 18 $("#Form :submit").click(function () { 19 var flag=true; 20 $("#Form .con").each(function () { 21 if ($(this).val().trim().length==0){ 22 var span=$("<span>"); 23 var mark=$(this).attr("mark"); 24 span.text(mark+"不能为空"); 25 26 $(this).after(span); 27 flag=false; 28 return false; 29 }; 30 31 }); 32 return flag 33 }); 34 </script> 35 </body> 36 </html>