【问题标题】:JQuery validation plugin: $("#").validate() is not a functionJQuery 验证插件:$("#").validate() 不是函数
【发布时间】:2011-02-25 06:11:04
【问题描述】:

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

<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>

在正文部分,我定义了表单和输入字段以接受重量值。正文中的代码如下:

<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 中收到以下错误:

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

我在网上搜索了一下,但找不到任何解决方案。我想我的 jquery 库位于正确的位置,而且 jquery-validation 插件似乎与 jquery-1.4.2 兼容。

关于我做错了什么有什么想法吗?

谢谢, 尼基尔。

【问题讨论】:

  • 抱歉这个问题,但您确定验证插件路径中的所有内容都是正确的?例如,您没有使用缩小版,是吗? jquery.validate.js
  • 嘿帕特里克,感谢您的回复。是的,我使用的是 jquery-1.4.2.js,而不是缩小版。

标签: jquery jquery-validate


【解决方案1】:

您在设置验证时并不完全正确。试试:

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

然后使用:

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

无论您想在哪里触发验证。

【讨论】:

  • 嗨 ProgrammingPope,感谢您的意见。我现在在页面加载时没有收到任何错误。但是,当我尝试在 createClicked() 函数中验证表单时,我得到了同样的错误:
【解决方案2】:

有同样的问题;经过验证的 HTML 并发现我缺少来自 &lt;input /&gt;name="email" 属性。始终验证 HTML 肯定您的 HTML 是有效的。我添加后,validate() 完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2018-02-25
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多