javascript jquery.validate 验证禁用字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6403332/
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 validating disabled field
提问by ed1t
I have a drop down in a form which controls which fields will be enabled/disabled on the form using jQuery's .show() and .hide(). In my jquery.validate rules, I have all the fields as required.
我在表单中有一个下拉列表,它控制使用 jQuery 的 .show() 和 .hide() 在表单上启用/禁用哪些字段。在我的 jquery.validate 规则中,我拥有所需的所有字段。
This is what my jquery.validate rule looks like
这就是我的 jquery.validate 规则的样子
$("#new_course").validate({
ignore: ":disabled",
rules: {
"course_init_on": {required: true},
"mins_before": {required: true},
.....
With the code, above it still validates the field that are hidden and stops the form from submitting.
使用上面的代码,它仍然验证隐藏的字段并阻止表单提交。
回答by Mo Valipour
What I can see is that you have added ignore ":disabled" which is different than ":hidden"
我可以看到的是,您添加了与“:hidden”不同的 ignore ":disabled"
I think you should use:
我认为你应该使用:
$("#new_course").validate({
ignore: ":hidden",
...
回答by Prydie
If you really want to disable the form elements (which you probably should else the browser will post that data) use the following:
如果您真的想禁用表单元素(您可能应该这样做,否则浏览器将发布该数据)使用以下内容:
$("#elm").attr("disabled", "disabled");
Then the above code will work perfectly and you wont be posting unnecessary data to the server :)
然后上面的代码将完美运行,您不会将不必要的数据发布到服务器:)
回答by Lapenkov Vladimir
Disabled is not checked in validate.js (hardcoded):
在validate.js(硬编码)中未检查禁用:
elements: function() {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
return $(this.currentForm)
.find("input, select, textarea")
.not(":submit, :reset, :image, [disabled]");
}
You have to use readonly
attribute instead
您必须改用readonly
属性
回答by Shef
Try:
尝试:
$("#new_course").validate({
ignore: ":hidden",
rules: {
"course_init_on": {required: true},
"mins_before": {required: true},
.....