日期:2014-05-19  浏览次数:20982 次

jquery-validate的用法
我有这样一段代码,想要表单验证,如果验证不通过,怎么显示在输入框旁边?<script>
JScript code
$(function(){
            $("#trainTypeForm").validate({
                rules:{
                    "trainType.trainTypeName":{
                        required:true
                    }
                    "trainType.trainPeriod":{
                        required:true,
                        number:true
                    }
                
                },
                message:{
                    "trainType.trainTypeName":{
                        required:"培训类型名称必须填写!",
                    }
                    "trainType.trainPeriod":{
                        required:"培训周期必须填写!",
                        number:"必须为数字"
                    }
                }
            
            });
        });
    
    </script>
         <body>
             <s:form id="trainTypeForm" method="post" action="">
               <table>
                <tr><td>
                    <s:textfield name="trainType.trainTypeName" id="trainTypeName">
                </td></tr>
               </table>
             </s:form>
         </body>



------解决方案--------------------
小做改动即可。。

JScript code

<script>
$(function(){
            $("#trainTypeForm").validate({
                rules:{
                    "trainType.trainTypeName":{
                        required:true
                    }
                    "trainType.trainPeriod":{
                        required:true,
                        number:true
                    }
                
                },
                message:{
                    "trainType.trainTypeName":{
                        required:"培训类型名称必须填写!",
                    }
                    "trainType.trainPeriod":{
                        required:"培训周期必须填写!",
                        number:"必须为数字"
                    }
                },
                errorPlacement: function(error, element) {           
               error.appendTo( element.parent());           
                }, 
            
            });
        });
    
    </script>

------解决方案--------------------
你看看这篇文章吧。。。
http://blog.csdn.net/lxf9601/article/details/5863728
上个月,我的项目中,领导也要求使用了jquery的validate框架。。。
我就是从上面那篇博客看到的。好使得很。。。
至于楼主你那里为什么不好使的话,要看看你代码里面的实际情况了。也许也和你使用的struts框架生成的一大堆垃圾代码有关系吧。。。


楼主,你好好研究研究这篇博客吧,相信你能解决问题的。。。