我是jQuery的新手,正在尝试将其用于注册页面的验证。这是我的HTML<div class="form-group"> <label for="inputFirstName" class="col-lg-3 control-label">First name</label> <div class="col-lg-6"> <input type="text" class="form-control" id="inputFirstName" name="inputFirstName" placeholder="First name"> <span class="" aria-hidden="true"></span> <-- this span </div></div>这是我的javascript$(document).ready(function () {$('#registration-form').validate({ rules: { inputFirstName: { required: true, }, }, highlight: function (element) { $(element).closest('.form-group').removeClass('has-success has-feedback').addClass('has-error has-feedback'); //want to remove 'glyphicon glyphicon-ok form-control-feedback' and then add some other class to span }, success: function (element) { $(element).closest('.form-group').removeClass('has-error has-feedback').addClass('has-success has-feedback'); //want to insert 'glyphicon glyphicon-ok form-control-feedback' to span }});});如果验证成功,我想将glyphicon glyphicon-ok form-control-feedback添加到跨度我尝试添加$(element).closest('span').addClass("glyphicon glyphicon-ok form-control- feedback");但这没有用。我该如何实现?谢谢 最佳答案 步骤1输入您的跨度ID    第2步成功内使用jquery的addClass()方法(如下所示)由IntelliJ IDEA创建。用户:RaghavFTW  日期:2015年4月7日  时间:15:13  要更改此模板,请使用File |设置|文件模板。-%>                                                            注册                                            名字                                                                                       placeholder =“名字”>                                                                                                                     姓氏                                                                                       placeholder =“姓氏”>                                                                                                                     用户名                                                                                       placeholder =“ Username”>                                                                                                                     密码                                                                                       placeholder =“ Password”>                                                                                                                     电子邮件                                                                                       placeholder =“ Email”>                                                                                                                     地址                                                                                          name =“ address”>                                                                                                                      性别                                                                                                                                                                   检查=“”>                                    男                                                                                                                                                                        女                                                                                                                                                                                取消                            提交                                                                                         $(document).ready(function(){        $('#registration-form')。validate({            规则:{                inputUsername:{                    最小长度:5                    必填:true                },                输入密码: {                    必填:是,                    最小长度:6                },                确认密码: {                    必填:是,                    最小长度:6                    equalTo:“ #password”                },                inputEmail:{                    必填:是,                    电子邮件:true                },                地址: {                    最小长度:10,                    必填:true                },                inputFirstName:“必需”,            },            高亮显示:功能(元素){                $(element).closest('。form-group')。removeClass('has-success has-feedback')。addClass('has-error has-feedback');             // var Identification = element.parent();             // console.log(identify);             // $(“ span [for ='” + identify +“']”)。removeClass(“ glyphicon glyphicon-ok form-control-feedback”);            },            成功:功能(元素){                $(element).closest('。form-group')。removeClass('has-error has-feedback')。addClass('has-success has-feedback');                $(element).remove();                var Identification = element.attr(“ for”);                console.log(identify);                $(“ span [for ='” + identify +“']”)。addClass(“ glyphicon glyphicon-ok form-control-feedback”);            }        });        $('#search-form')。validate({            规则:{                搜索:“必填”            },            高亮显示:功能(元素){                $(element).closest('。form-group')。removeClass('has-success')。addClass('has-error');            },            成功:功能(元素){                $(element).closest('。form-group')。removeClass('has-error')。addClass('has-success');            }        });        $(“ input,textarea”)。change(function(){            varidentify = this.id;            console.log(identify);            $(“ span [for ='” + identify +“']”)。removeClass(“ glyphicon glyphicon-ok form-control-feedback”);        });    })    ; //结束document.ready关于javascript - 如果验证成功,如何将span类添加到span元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31221364/
10-13 00:36