jQuery验证插件:$(“#").validate()不是函数 [英] JQuery validation plugin: $("#").validate() is not a function

查看:92
本文介绍了jQuery验证插件:$(“#").validate()不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery-1.4.2库和jquery.validate.js插件来验证一个非常简单的表单.在开头部分,我有以下代码:

I am using jquery-1.4.2 library and jquery.validate.js plugin to validate a very simple form. In the head section, I have following code:

<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.js"></script>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.2.custom.css"></link>
<script type="text/javascript" src="scripts/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript">
     $(document).ready(function(){
    // date pickers
    $('#birthdate').datepicker();
    $("#deliverydate").datepicker();

    //apply styles to buttons 
    $('input:button').button();

    // data validation
    $("#createAnimalForm").validate({
        debug: true,
        rules: {
            weight: {
                required: true
            }
        },
        messages: {
            weight: {
                     required: "Please enter a numeric value for weight."
                    }
        }
    });

});
function createClicked(){
    $("#createAnimalForm").validate().form();
}


</script>

在主体部分,我定义了形式和输入字段以接受重量值.来自正文的代码如下:

In the body section, I have defined the form and the input field to accept the weight value. The code from body is as follows:

<form id="createAnimalForm">
<table cellpadding="5px">
<tr>
    <td>Weight:</td>
    <td><input type="text" name="weight" id="weight"></input>&nbsp;gms</td>
</tr>
<tr>
    <td></td>
    <td align="left"><input type="button" onclick="createClicked();" id="createButton" name="createButton" value="Create Animal"></td>
</tr>
</table>
</form>

在firefox中加载页面时,在firebug中出现以下错误:

When I load the page in firefox, I get the following error in firebug:

$("#createAnimalForm").validate is not a function

我在网上搜索了一下,但找不到任何解决方案.我认为我的jquery库位于正确的位置,看来jquery-validation插件与jquery-1.4.2兼容.

I searched on the net for a bit, but could not find any solution for this. I think I have the jquery library located at the correct location, and it appears that jquery-validation plugin is compatible with jquery-1.4.2.

关于我在做什么错的任何想法吗?

Any ideas on what I am doing wrong?

谢谢, 尼基尔.

推荐答案

在设置验证时,您不太正确.试试:

You don't quite have it right when you are setting up validate. Try:

    // data validation
$("#createAnimalForm").validate({
    debug: true,
    rules: {
        weight: {
            required: true
        }
    },
    messages: {
            weight: {
              required: "your message here"
            }
    }
});

然后使用:

if ($('#createAnimalForm').validate().form()) { ... }

您想触发验证的任何地方.

Wherever you want to trigger validation.

这篇关于jQuery验证插件:$(“#").validate()不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆