Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option includeDisabled #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Available options:
* **defaultTypes: {defaults}**, contains the orignal type functions `string`, `number`, `boolean`, `null`, `array`, `object` and `skip`.
* **defaultType: "string"**, fields that have no `:type` suffix and no `data-value-type` attribute are parsed with the `string` type function by default, but it could be changed to use a different type function instead.
* **disableColonTypes: true**, do not parse input names as types, allowing field names to use colons. If this option is used, types can still be specified with the `data-value-type` attribute. For example `<input name="foo::bar" value="1" data-value-type="number">` will be parsed as a number.
* **includeDisabled: false**, if true, disabled :input's included.

More details about these options in the sections below.

Expand Down
26 changes: 17 additions & 9 deletions jquery.serializejson.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* --- */
/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 3.2.1 (Feb, 2021)
version 3.2.2 (Mar, 2021)

Copyright (c) 2012-2021 Mario Izquierdo
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
Expand Down Expand Up @@ -50,6 +51,10 @@
if (type === "skip") {
return; // ignore fields with type skip
}
if(obj.multiple) {
type = 'array';
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check? Maybe we should explain in a comment for clarity


if (!type) {
type = opts.defaultType; // "string" by default
}
Expand Down Expand Up @@ -108,7 +113,8 @@
"disableColonTypes",
"customTypes",
"defaultTypes",
"defaultType"
"defaultType",
"includeDisabled"
];
for (var opt in options) {
if (validOpts.indexOf(opt) === -1) {
Expand All @@ -133,10 +139,9 @@
}).filter(function() {
var $el = $(this);
var type = this.type;

// Filter with the standard W3C rules for successful controls: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
return this.name && // must contain a name attribute
!$el.is(":disabled") && // must not be disable (use .is(":disabled") so that fieldset[disabled] works)
(!$el.is(":disabled") || $el.is(":disabled") && opts.includeDisabled) && // must not be disable (use .is(":disabled") so that fieldset[disabled] works)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check seems equivalent to (!$el.is(":disabled") || opts.includeDisabled)

rsubmittable.test(this.nodeName) && !rsubmitterTypes.test(type) && // only serialize submittable fields (and not buttons)
(this.checked || !rcheckableType.test(type) || f.getCheckboxUncheckedValue($el, opts) != null); // skip unchecked checkboxes (unless using opts)

Expand All @@ -154,13 +159,14 @@
}

if (isArray(val)) {
return $.map(val, function(val) {
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };
} );
if(!val.length) {
return { name: el.name, el: el }
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would the array not have a length property? Should that not be factored into the isArray(val) function check?

return $.map(val, function(val) {
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el};
} );
}

return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };

}).get();
},

Expand Down Expand Up @@ -336,3 +342,5 @@
var isValidArrayIndex = function(val) { return /^[0-9]+$/.test(String(val)); }; // 1,2,3,4 ... are valid array indexes
var isArray = Array.isArray || function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; };
}));