jQuery 验证插件和 Colorbox
我使用 Colorbox 在模态窗口中打开注册表单和登录表单
不幸的是,jQuery 验证插上就不能用了。我进行了搜索,发现有人在 Stack Overflow 上提出了一个问题,其中包含我需要的答案 。我还不太熟悉 JavaScript,所以在实现该解决方案时遇到了一些问题。
我尝试解释我的代码上的解决方案,但无法弄清楚。我的代码如下
$(document).ready(function(){
$('.popup').colorbox();
$('#register form').validate({
rules: {
username: {
required: true,
},
email: {
required: true,
email: true
},
password: {
minlength: 4,
required: true
},
password2: {
equalTo: "#password"
}
},
success: function(label) {
label.text('OK!').addClass('valid');
}
});
$('#login form').validate({
rules: {
username: {
required: true
},
password: {
required: true
}
}
})
: });
有人可以澄清我如何实施该解决方案吗?谢谢。
I am opening up both a registration form and a login form in a modal window using Colorbox
Unfortunately, the jQuery validate plug in doesn't work anymore. I did a search and found someone a question with the answer I needed here at Stack Overflow. I'm not really well versed with JavaScript yet so I had a bit of a problem implementing the solution.
I tried interpreting the solutions on my code but couldn't figure it out. My code is as follows:
$(document).ready(function(){
$('.popup').colorbox();
$('#register form').validate({
rules: {
username: {
required: true,
},
email: {
required: true,
email: true
},
password: {
minlength: 4,
required: true
},
password2: {
equalTo: "#password"
}
},
success: function(label) {
label.text('OK!').addClass('valid');
}
});
$('#login form').validate({
rules: {
username: {
required: true
},
password: {
required: true
}
}
})
;
});
Can somebody please clarify how I can implement the solution a bit? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我仔细研究了一下,发现了这个解决方案:
加载异步或替换页面中的内容后,您可以
调用此 JQuery 插件方法:
$.validator.unobtrusive.addValidation("#formID");
这将删除现有的验证属性并在指定表单中分配新的一次,或者如果您更改“#formID ” 到“form”,它将对页面中的所有表单执行此操作。
确保将此代码添加到页面脚本中:
可以在此处找到带有说明的原始解决方案:
http://www.mfranc.com/2011/ 07/04/unobtrusive-validation-in-partial-views/
I did some goggling my self and I found this solution:
After you load your Async or replace content in the page, you have
to call this JQuery Plugin method:
$.validator.unobtrusive.addValidation("#formID");
This will remove existing validation attributes and assign new once in the specified form, or if you change "#formID" to "form" it will do that for all forms in the page.
Make sure you add this code to the page script:
Original solution with explanation can be found here:
http://www.mfranc.com/2011/07/04/unobtrusive-validation-in-partial-views/
这是我使用 JQuery Validation 插件和 Colorbox 时的实现。我所做的是先编译错误,然后在 Colorbox 上显示一次。
This is my implementation when using JQuery Validation plugin and Colorbox. What I'm doing is that I compile the errors first then display it once on Colorbox.
没关系。我想通了。看起来我缺少一个大括号!代码如下:
});
Nevermind. I figured it out. Looks like I was missing a curly bracket! The code came out to:
});