<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!--[if lt IE 9]> <script src="../bootstrap/html5shiv.min.bootstrap"></script> <script src="../bootstrap/respond.min.bootstrap"></script> <![endif]--> <script src="../scripts/jquery-1.12.2.min.js"></script> <script src="../bootstrap/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-6 col-sm-offset-3"> <h2 class="page-header">注册</h2> <form class="form-horizontal" action="bootstrap表单带验证.html" method="get"> <div class="form-group has-feedback"> <label for="email" class="col-sm-2 control-label">Email </label> <div class="col-sm-10"> <input type="email" class="form-control" id="email" placeholder="Email" required> <span class=""></span> </div> </div> <div class="form-group has-feedback"> <label for="username" class="col-sm-2 control-label">username</label> <div class="col-sm-10"> <input type="text" class="form-control" id="username" placeholder="username" required> <span class=""></span> </div> </div> <div class="form-group has-feedback"> <label for="password" class="col-sm-2 control-label">password</label> <div class="col-sm-10"> <input type="password" class="form-control" id="password" placeholder="password" required> <span class=""></span> </div> </div> <p class="text-right"> <button id="btnSubmit" type="submit" class="btn btn-primary">submit</button> <button type="reset" class="btn btn-danger">reset</button> </p> </form> </div> </div> </div> <script> function checkInput(txt){ if(txt.val()==""){ // alert(txt.parent().parent().html()); alert html txt.parent().parent() /*找到父类 先删除样式,再添加样式*/ .removeClass() .addClass("form-group has-error has-feedback"); txt.next().removeClass() .addClass("glyphicon glyphicon-remove form-control-feedback"); return false; }else{ txt.parent().parent().removeClass().addClass("form-group has-success has-feedback"); txt.next().removeClass().addClass("glyphicon glyphicon-ok form-control-feedback"); return true; } } function checkBind(id){ $(id).bind("blur", function () { checkInput(id); }); } $().ready(function () { checkBind($("#email")); checkBind($("#username")); checkBind($("#password")); /* $("#email").bind("change", function () { checkInput($("email")); });*/ /* $("#email").change(function () {checkInput($("#email"));}); $("#username").change(function () {checkInput($("#username"));}); $("#password").change(function () {checkInput($("#password"));});*/ $("#btnSubmit").bind('click', function () { if(checkInput($("#email"))&&checkInput($("#username"))&&checkInput($("#password"))){ return true; } return false; }) }); </script> </body> </html>