最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery.validate validating disabled field - Stack Overflow

programmeradmin4浏览0评论

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.

This is what my jquery.validate rule looks like

$("#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.

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.

This is what my jquery.validate rule looks like

$("#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.

Share Improve this question asked Jun 19, 2011 at 15:33 ed1ted1t 8,70917 gold badges70 silver badges112 bronze badges 1
  • 1 Being "hidden" is not the same as being "disabled". – Pointy Commented Jun 19, 2011 at 15:35
Add a ment  | 

4 Answers 4

Reset to default 10

What I can see is that you have added ignore ":disabled" which is different than ":hidden"

I think you should use:

$("#new_course").validate({
ignore: ":hidden",
...

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 :)

Disabled is not checked in validate.js (hardcoded):

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

Try:

$("#new_course").validate({
ignore: ":hidden",
rules: {
"course_init_on": {required: true},
"mins_before": {required: true},
    .....
发布评论

评论列表(0)

  1. 暂无评论