Skip to content

Commit

Permalink
make sure we make an empty array if all checkboxes are unset
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
KATT committed Oct 6, 2014
1 parent a0be870 commit dc74490
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion jquery.serializejson.js
Expand Up @@ -164,19 +164,42 @@
}
},

formAsArrayContainsName: function(array, name) {
var len = array.length;
var altName = name + '[]';
for (var i = 0; i < array.length; i++) {
if (array[i].name === name || array[i].name === altName) {
return true
}
}
return false;
},

// Fill the formAsArray object with values for the unchecked checkbox inputs,
// using the same format as the jquery.serializeArray function.
// The value of the uncheked values is determined from the opts.checkboxUncheckedValue
// The value of the unchecked values is determined from the opts.checkboxUncheckedValue
// and/or the data-unchecked-value attribute of the inputs.
readCheckboxUncheckedValues: function (formAsArray, $form, opts) {
var selector, $uncheckedCheckboxes, $el, dataUncheckedValue, f;
if (opts == null) opts = {};
f = $.serializeJSON;

selector = 'input[type=checkbox][name]:not(:checked)';

$uncheckedCheckboxes = $form.find(selector).add($form.filter(selector));
$uncheckedCheckboxes.each(function (i, el) {
$el = $(el);
if (!f.isUndefined(opts.checkboxUncheckedValue) && el.name.substr(-2, 2) === '[]') {
// @see https://github.com/marioizquierdo/jquery.serializeJSON/issues/25
var arrayName = el.name.substr(0, el.name.length - 2);
if (!serializeJSON.formAsArrayContainsName(formAsArray, arrayName)) {
formAsArray.push({
name: arrayName,
value: []
});
}
return;
}
dataUncheckedValue = $el.attr('data-unchecked-value');
if(dataUncheckedValue) { // data-unchecked-value has precedence over option opts.checkboxUncheckedValue
formAsArray.push({name: el.name, value: dataUncheckedValue});
Expand Down

0 comments on commit dc74490

Please sign in to comment.