jQuery Validate() 和 Valid() 方法未定义或不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19630075/
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() and Valid() methods undefined or do not work
提问by Hyman
I use jQuery validation in my MVC4 project with the necessary javascript files (jquery-1.9.1.js, jquery.validate.min.jsand jquery.validate.unobtrusive.min.js) and configuration. What I want to do is to validate the form before submitting it. If the form is valid, a method will be called and then it will be submitted. If not, it will not be submitted and simply an alert will be displayed. I have followed the steps on Call JQuery Validate Plugin without submitting the formand lots of similar topics especially on the jQuery offical pages. But somehow, when executing the Validate() and Valid() method I encountered errors like undefined. I think the problem is related to Validate() method. I have tried different kind of jquery-1.x.x.js files, but the result is the same. How can I validate the form by using a similar method to the method below? Or anything else?
我在我的 MVC4 项目中使用 jQuery 验证和必要的 javascript 文件(jquery-1.9.1.js、jquery.validate.min.js和jquery.validate.unobtrusive.min.js)和配置。我想要做的是在提交之前验证表单。如果表单有效,则将调用一个方法,然后将其提交。如果没有,则不会提交,只会显示警报。我已经按照Call JQuery Validate Plugin上的步骤操作,而没有提交表单以及许多类似的主题,尤其是在 jQuery 官方页面上。但不知何故,在执行 Validate() 和 Valid() 方法时,我遇到了像 undefined 这样的错误。我认为问题与 Validate() 方法有关。我尝试了不同类型的 jquery-1.xxjs 文件,但结果是一样的。如何使用与以下方法类似的方法验证表单?或者别的什么?
script type="text/javascript">
$(function () {
$("#submitbtn").click(function () {
var form = $("#myForm");
form.validate();
var isValid = form.valid();
if (isValid) { //If there is no validation error
alert('form is valid - not submitted');
}
else {
alert('form is not valid');
}
});
});
采纳答案by Hyman
Thanks a lot all of you. Finally I have managed to combine validation methods with the spinner example posted on: http://blog.tkglaser.net/2012/02/waiting-spinner-for-long-running-form.html
非常感谢大家。最后,我设法将验证方法与发布在以下位置的微调器示例相结合:http: //blog.tkglaser.net/2012/02/waiting-spinner-for-long-running-form.html
View:
看法:
<style type="text/css">
#loading {
display: none;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.8);
z-index: 1000;
}
#loadingcontent {
display: table;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#loadingspinner {
display: table-cell;
vertical-align: middle;
width: 100%;
text-align: center;
font-size: larger;
padding-top: 80px;
}
</style>
$(function () {
//Waiting spinner for long-running form submits using jQuery
$("#submitbtn").click(function () {
var form = $("#addForm");
form.validate();
if (form.valid()) {
$("#loading").fadeIn();
var opts = {
lines: 12, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 10, // The radius of the inner circle
color: '#000', // #rgb or #rrggbb
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false // Whether to use hardware acceleration
};
var target = document.getElementById('loading');
var spinner = new Spinner(opts).spin(target);
}
});
});
<div id="loading">
<div id="loadingcontent">
<p id="loadingspinner">
Waiting...
</p>
</div>
</div>
回答by Wibble
I found the code was working perfectly fine however the ID of the element was not "myForm", have you tried ensuring that the ClientID attribute of the form is used rather than the ID?
我发现代码运行良好,但是元素的 ID 不是“myForm”,您是否尝试过确保使用表单的 ClientID 属性而不是 ID?
$(function () {
$("#submitbtn").click(function () {
var form = $('#<%# myForm.ClientID %>');
form.validate();
var isValid = form.valid();
if (isValid) { //If there is no validation error
alert('form is valid - not submitted');
}
else {
alert('form is not valid');
}
});
});
I believe $("#myForm")
wasn't returning the element as you would have expected (e.g. it was returning null or undefined as myForm didn't exist) hence form.validate() wouldn't exist.
我相信$("#myForm")
没有像您预期的那样返回元素(例如它返回 null 或 undefined 因为 myForm 不存在)因此 form.validate() 将不存在。
回答by Chris Wu
What is the exact error message you got? Maybe you are not referencing jquery properly.
Try do a simple jquery call like alert($("#myInput").val());
您收到的确切错误消息是什么?也许您没有正确引用 jquery。尝试做一个简单的 jquery 调用,比如alert($("#myInput").val());