Using custom CSS with jQuery validation? [closed]

烈酒焚心 提交于 2019-12-13 22:54:39

问题


How can I use a custom CSS class for the error messages? I'd rather not style the standard label.error. I want to use a CSS class I already have.


回答1:


There is an option for jQuery Validate called errorClass:

Use this class to create error labels, to look for existing error labels and to add it to invalid elements.

All you need to do is specify it in your original $.validate call:

$('#myForm').validate({
   errorClass:'myClass'
   //your options
});



回答2:


You could do something like that:

html:

<form action="">
    <ul>
        <li><input type="text" id="name" value="Your name" /></li>
        <li><input type="submit" id="submit" /></li>
        <li class="error">
            <span>Ops!</span>
        </li>
    </ul>
</form>

css:

form .error {display:none;}

jquery:

$('form').submit(function(){
    if($('#name').val()==''||$('#name').val()=='Your name'){
        $('form .error').css({display: 'block'});
    }
})


来源:https://stackoverflow.com/questions/14447295/using-custom-css-with-jquery-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!