javascript document.ready() 的 jQuery 异常 - 对象不支持此属性或方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11420848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:09:01  来源:igfitidea点击:

jQuery exception with document.ready() - Object doesn't support this property or method

javascriptjqueryasp.net-mvc

提问by Bob Horn

I'm trying to perform some simple validation, and I'm following some instructions in the book JavaScript & jQuery, The Missing Manual. My code is simply this:

我正在尝试执行一些简单的验证,并且我正在遵循JavaScript & jQuery, The Missing Manual一书中的一些说明。我的代码很简单:

<script src="~/Scripts/jquery-1.6.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>

<form> form code here </form>

<script>
    $(document).ready(function() {
        $('#vaporizerForm').validate();
    });
</script>

And I'm getting this error:

我收到此错误:

Unhandled exception at line 1039, column 15 in /Scripts/jquery-1.6.2.js

0x800a01b6 - Microsoft JScript runtime error: Object doesn't support this property or method

/Scripts/jquery-1.6.2.js 中第 1039 行第 15 列的未处理异常

0x800a01b6 - Microsoft JScript 运行时错误:对象不支持此属性或方法

If I remove the $(document).ready part, and just do this, it works:

如果我删除 $(document).ready 部分,然后执行此操作,它会起作用:

<script>
        $('#vaporizerForm').validate();
</script>

Any idea why the $(document).ready() part isn't working?

知道为什么 $(document).ready() 部分不起作用吗?

EDIT -- jQuery call stack and code

编辑——jQuery 调用堆栈和代码

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

EDIT - Entire code in the view

编辑 - 视图中的整个代码

@model IEnumerable<DistributorManagement.Models.Vaporizer>

@{
    ViewBag.Title = "Vaporizer";
}
@{
    var grid = new @WebGrid(
        source: Model,
        rowsPerPage: 10);
}

<script src="~/Scripts/jquery-1.6.2.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.js" type="text/javascript"></script>

<script>
    $(document).ready(function () {
        $('#vaporizerForm').validate();
    });
</script>

<form action="#" method="post" name="vaporizerForm" id="vaporizerForm">
    <div>
        <label>Manufacturer</label>
        <input name="manufacturer" type="text" class="required" />

        <label>BuildDate</label>
        <input name="buildDate" type="text" class="required date" />

        <label>Rating</label>
        <input name="rating" type="text" class="required number" />
    </div>
    <div>
        <input type="submit" name="submit" value="Submit" />
    </div>
</form>

<br />
<h1>Vaporizer Info</h1>
<div class="webgrid-wrapper">
    <div id="grid">
        @grid.GetHtml(
            tableStyle: "webgrid",
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-rows",
            columns: grid.Columns(
                grid.Column("Id", "ID"),
                grid.Column("Manufacturer"),
                grid.Column("Status"),
                grid.Column("BuildDate", "Build Date"),
                grid.Column("Rating")))
    </div>
</div>

回答by Hogan

I'll go for the obvious answer, are you sure the file at ~/Scripts/jQuery/jquery.validate.jsis ok? It is acting like that file is empty, or broken.

我会去寻找明显的答案,你确定文件在~/Scripts/jQuery/jquery.validate.js吗?它的行为就像该文件是空的或损坏的。

To check if the file is ok, navigate to that directory observe the file and open it in the editor, does it have any content.

要检查文件是否正常,导航到该目录观察文件并在编辑器中打开它,它是否有任何内容。

If you take out the document ready stuff you probably need the following code to see an error:

如果您取出文档准备好的东西,您可能需要以下代码才能看到错误:

 try {
   $('#vaporizerForm').validate();
 }
 catch ()  {
   alert('still have error');
 }

回答by Nullpo

Try with this:

试试这个:

<script type="text/javascript" src="~/Scripts/jquery.validate.js"></script>

-- EDITED --

-- 编辑 --

And put the $(document).ready(.......) in the bottom of your page.

并将 $(document).ready(.......) 放在页面底部。

This happens with some versions of IE, when your page is too long.... put it before the body !

某些版本的 IE 会发生这种情况,当您的页面太长时.... 将它放在正文之前!

回答by Drewness

Put your validation script before the <form>.

将您的验证脚本放在<form>.

<script src="~/Scripts/jquery-1.6.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script>
  $(document).ready(function(){
    $('#vaporizeForm').validate();
  });
</script>

<form> form code here </form>

Hereis a fiddle as an example.

这里有一个小提琴作为例子。