<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"/> <title>Title</title> <style> .error{ color: red; } </style> </head> <body> <form id="ff" method="post"> <div><input type="text"/></div> <div><input type="password"/></div> <div><input type="text"/></div> <div><input type="text"/></div> <div><input type="text"/></div> <div><input type="submit" value="提交"/></div> </form> <script src="jquery-1.12.4.js"></script> <script> $(‘:submit‘).click(function () { $(‘.error‘).remove() var flag = true; $(‘#ff‘).find(‘input[type="text"],input[type="password"]‘).each(function () { var tag = $(this).val(); if (tag.length <= 0) { flag = false; var v= document.createElement(‘span‘); v.innerHTML = ‘必填‘; v.className=‘error‘; $(this).after(v) return false // 只提示第一个 return false } }) return flag; }) </script> </body> </html>
提交表单验证
时间: 2024-10-16 18:51:25