javascript jQuery 验证:未捕获的类型错误:$(...)."valid is not a function" with valid references/order
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46800748/
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
jQuery validate: Uncaught TypeError: $(...)."valid is not a function" with valid references/order
提问by James
I'm getting the error message "Uncaught TypeError: $(...).valid is not a function" when attempting to call...
尝试调用时,我收到错误消息“Uncaught TypeError: $(...).valid is not a function”
javascript
javascript
$('input[data-val=true]').on('blur', function() {
$(this).valid();
});
HTML
HTML
<div class="col-xs-12 col-sm-5" style="margin-left:4px">
<input required="True" class="form-control" data-bind="value: battleModel.ReserveTroopCount" data-val="true" data-val-number="The field Troop reserve must be a number." data-val-range="Troop reserve must be between 0 and 1000." data-val-range-max="1000" data-val-range-min="0" data-val-required="Troop reserve is required." id="ReserveTroopCount" name="ReserveTroopCount" placeholder="Troop reserve..." tabindex="1" title="Enter the minimum troops you want to reserve after the battle" type="text" value="5">
<span class="field-validation-valid" data-valmsg-for="ReserveTroopCount" data-valmsg-replace="true"></span>
</div>
References
参考
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/Content/bootstrap.css" rel="stylesheet" />
<script src="@(Url.Content("/Scripts/jquery-2.2.4.js"))" type="text/javascript"></script>
<script src="@(Url.Content("/Scripts/jquery.validate.js"))" type="text/javascript"></script>
<script src="@(Url.Content("/Scripts/jquery.validate.unobtrusive.js"))" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="~/Scripts/bootstrap-toolkit.js"></script>
@Scripts.Render("http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.4.2.js")
<script src="../../Scripts/knockout.mapping-latest.js" type="text/javascript"></script>
<script src="@(Url.Content("~/Scripts/RRCommon.js"))" type="text/javascript"></script>
<script src="@(Url.Content("~/Scripts/StrUtil.js"))" type="text/javascript"></script>
<script src="@(Url.Content("~/Scripts/RRRoll.js"))" type="text/javascript"></script>
My jquery files are local, but they are valid. Just pulled them off of NuGet today. I see all of my scripts loaded in the browser...
我的 jquery 文件是本地的,但它们是有效的。今天刚刚将它们从 NuGet 中删除。我看到浏览器中加载了所有脚本...
Screenshot of browser dev tools displaying my script sources
What am I missing? Everything I've seen online suggests either the script reference is missing or they're in the wrong order. Not sure what else to do. The error suggests the referenced jquery files aren't getting loaded. I also tried online cdn references to the jquery files but got the same error.
我错过了什么?我在网上看到的一切都表明脚本参考丢失或它们的顺序错误。不知道还能做什么。该错误表明未加载引用的 jquery 文件。我还尝试了对 jquery 文件的在线 cdn 引用,但遇到了同样的错误。
采纳答案by Derek Brown
In order to use the jQuery form validation plugin, you need to set it up on your form first. The documentationgives a few examples of this, but basically you need to select the form and run the .validate()function.
为了使用 jQuery 表单验证插件,您需要先在表单上进行设置。该文档提供了一些示例,但基本上您需要选择表单并运行该.validate()功能。
var form = $( "#myform" );
form.validate();
Obviously you don't have a form element, so this is something you will need to add.
显然你没有表单元素,所以这是你需要添加的东西。

