I want to get a jQuery object representing the exact collection of elements whose data would actually be submitted. So, part one is how does the browser decide which fields to submit, and part two is how to get jQuery to select that set. (I've found many answers on actively adding to or subtracting from the set of submittables, but almost nothing on passively determining them.)
Ideally there's a closed-form selector expression for these elements. The best I've come up with however has an elaborate .filter() function.
var $submittables = $('#myform').find('[name]:input:not(:button)').filter(function () {
if ($(this).is(':checkbox:not(:checked), :radio:not(:checked)')) {
return false;
}
return !$(this).prop('disabled');
});
Is this accurate? Is there a leaner way?
I gather $('#myform').serializeArray()
will get the name-value pairs rather succinctly, but that's not simple to translate into a collection of elements, e.g. with only the checked radio buttons and checkboxes.
The goal is to be able to iterate through these form elements with .each(). My own application is to clone or disable some fields at submit-time based on data properties, so I want the jQuery objects, not just the name-value pairs. Here's an oversimplified use of the collection:
$submittables.each(function () {
console.log("name", this.attr('name'), "value", this.val());
});
The answers to "How to loop through all elements of a form jQuery" either include elements that will not be submitted, or provide just the name-value pairs.