OK, I've made several modifications thanks to your suggestions and
corrections. This is publishable; should I go ahead and create a new
wiki page under Plugins or maybe wait until it's decided whether this
will be merged somehow with the existing form plugin?

m.

---

$.fn.fastSerialize = function() {
    var a = [];

    $('input,textarea,select,button', this).each(function() {
        var n = this.name;
        var t = this.type;

        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') &&
this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex
== -1)
            return;

        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );

        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }

        a.push({name: n, value: this.value});
    });

    return a;
};



On Mon, 2006-10-02 at 21:02 -0800, Matt Grimm wrote:
> Hello,
> 
> I've put together a fast form serializer function that I'm hoping can
> get some review from the list for completeness, bugs, a better name,
> etc. A previous thread revealed quite a performance issue with the
> form
> plugin's existing serialize function when operating on a form with a
> large number of elements. My plugin sacrifices semantic order for
> wicked
> speed. I have a test form with a select menu containing 500 options --
> with the current serializer, it takes around 5 full seconds to do the
> job, and with mine, it takes about 50 ms. Any feedback would be much
> appreciated, including whether you think this should be included with
> the form plugin distribution.
> 
> Since this isn't a plugin, I'm just going to include the source in
> this
> message -- please let me know if there's a better (i.e., more polite)
> way to do this.
> 
> Thanks!
> 
> m.
> 
> ---
> 
> $.fn.fastSerialize = function() {
>     var a = [];
>     var f = ['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'];
> 
>     $(f.join(','), this).each(function() {
>         var n = this.name || this.id;
>         var t = this.type;
> 
>         if (!n || this.disabled || t == 'reset' ||
>            (t == 'checkbox' || t == 'radio') && !this.checked ||
>            (t == 'submit' || t == 'image' || t == 'button') &&
> this.form.clicked != this ||
>            this.tagName == 'SELECT' && this.selectedIndex == -1)
>             return;
> 
>         if (t == 'image' && this.form.clicked_x)
>             return a.push(
>                 {name: n+'_x', value: this.form.clicked_x},
>                 {name: n+'_y', value: this.form.clicked_y}
>             );
> 
>         if (t == 'select-multiple') {
>             $('option', this).each( function() {
>                 if (this.selected)
>                     a.push({name: n, value: this.value ||
> $(this).text()});
>             });
>             return;
>         }
> 
>         a.push({name: n, value: this.value});
>     });
> 
>     return a;
> };
> 
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
> 
> 

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to