Skip to content

Conversation

tedliang
Copy link

I propose to accept a callback function in the serializeArray, so that the users can customize or filter the returning object.

For example:

$('#form').serializeArray(function(val){
  return {label:this.name, value:val, type:this.type};
});

This produces the following data structure:

[{label: "a", value: "1", type: "text"},
 {label: "b", value: "2", type: "text"},
 {label: "c", value: "3", type: "hidden"},
 ....]

More powerful one:

$('#form').serializeArray(function(val){
  var $elem = $(this);
  if($elem.hasClass('invalid')){
    return null;
  }
  else if($elem.is(':date')){
    val= mySpecialDateFormat(val);
  }
  else if($elem.is(':hidden')){
    mySpecialMethodToHandleHiddenInput(this.name, val);
  }
  return {name:this.name, value:val};
});

@tedliang tedliang closed this Aug 11, 2012
@tedliang tedliang reopened this Aug 11, 2012
@rwaldron
Copy link
Member

Can you provide tests that support this feature?

@@ -24,9 +27,9 @@ jQuery.fn.extend({
null :
jQuery.isArray( val ) ?
jQuery.map( val, function( val, i ){
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
return func( elem, val.replace( rCRLF, "\r\n" ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the convertion be handled inside the default function , so one can decicde per use case if it is required?

@tedliang
Copy link
Author

This optional converted value passed to the user is for consistency and convenience, since the rCRLF is defined inside jQuery. It's desired in most of use cases. One can retrieve the original value by calling $(this).val() explicitly.

@tedliang tedliang closed this Aug 12, 2012
@tedliang tedliang reopened this Aug 12, 2012
@dmethvin
Copy link
Member

We voted not to land this feature in core.

@dmethvin dmethvin closed this Oct 30, 2012
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants