jQuery: Form Plugin, Sweet… Almost

November 8, 2006

jqueryI am a recent convert to the powers of jQuery ever since Matt fell in love with it at "The Ajax Experience". The syntax and simplicity exceeds the combination of Prototype and Script.aculo.us that I was using.

By far one of the greatest features of jQuery is the ability for the community to create amazingly useful plugins. As I become dependent on many, I will highlight them here (mostly so I don't forget). The plugin I want to talk about right now is the form plugin from malsup.

Basically this plugin allows you to have a form be submitted through an XHR call. It uses the form's defined action URL to determine where to post the values and by default will update a specified div with returned data. It has additional flexibility to get returned data in XML or JSON if you would prefer. Go check out the example on his site, this is extremely simple to implement. This is exactly what I was looking for and I was quite happy, until I found a bug...

There is a problem with the internal formToArray() function when dealing with multi-select lists. If you are pre-selecting values in the list on load with some sort of server side language (PHP in my case), the pre-selected values will be submitted along with the newly selected user values. Obviously this is not what you would want to happen. The problem code occurs around 300 lines in:

JavaScript:
  1. if (t == 'select-multiple') {
  2.     jQuery('option:selected', this).each( function() {
  3.     a.push({name: n, value: this.value});
  4.     });
  5.     return;
  6. }

The jQuery search is finding more than it should. I have two ways in mind to resolve this issue. Most simply, you can add an if statement before the push, like this:

JavaScript:
  1. if (t == 'select-multiple') {
  2.     jQuery('option:selected', this).each( function() {
  3.     if(this.selected == true)
  4.         a.push({name: n, value: this.value});
  5.     });
  6.     return;
  7. }

This makes sense and is simple, yet it feels dirty since you receive more data from the search than necessary. I'm not certain about the efficiency, so here is another option:

JavaScript:
  1. if (t == 'select-multiple') {
  2.     for(var i=0;i<this.options.length;i++) {
  3.         if(this.options[i].selected==true)
  4.             a.push({name: n, value: this.options[i].value});
  5.     }
  6.     return;
  7. }

In this second one, we loop over all the elements in the multi-select to determine which are checked.

Like I said, I'm unsure which would yield the better performance, so hopefully someone will do the benchmarking and it can be rolled into the next version of this sweet plugin.

ajax, ajax experience, ajax form, form plugin, javascript, jquery, jquery form plugin, jquery plugin, jquery plugins, malsup, plugins, xhr

Tags: , , , , , , , , , , ,

Related:


Comments

One Response to “jQuery: Form Plugin, Sweet… Almost”

  1. malsup on November 9th, 2006 12:27 pm

    Thanks for finding this bug and for providing a nice solution. The plugin has been updated and all is well!

    http://jquery.com/dev/svn/trunk/plugins/form/form.js?format=txt

Got something to say?





User contributed tags: t (1740) - jquery forms (621) - jQuery $form (576) - jquery post (298) - jquery select (224) - jquery +selected (223) - jquery calendar (171) - jquery find (140) - jquery select value (118) - jquery selectedIndex (113) - form jquery (113) - jquery form plugin (107) - jquery checked (105) - jquery select option (91) - jquery multiselect (87) - jquery get select value (80) - jquery search (76) - jquery ajax form (74) - jquery option (72) - jquery option selected (71) - jquery select multiple (71) - jquery post form (65) - jquery selected option (61) - jquery json (59) - jquery selectbox (54) - jquery :options (53) - jquery get selected option (52) - jquery if statement (49) - jquery formToArray (48) - jquery get value (45) - jquery $.post example (42) - jquery form select (41) - jquery form post (40) - f (40) - jquery highlight (38) - jquery select form (38) - forms +jquery (37) - jquery select Options (37) - jquery get (37) - jquery multiple select (35) - jquery form value (35) - jquery contact form (34) - jquery get form value (33) - jquery multi select (33) - jquery select box (33) - json jquery (31) - jQuery get selected value (31) - jquery nice forms (30) - jquery and forms (29) - jquery (28) - jquery select list (27) - jquery.post (26) - jquery getvalue (26) - jquery torrent (26) - $ post JQuery (25) - jquery nice form (25) - jquery selected value (25) - jquery form builder (24) - jquery value() (23) - jquery option[@selected] (22) - jquery form selector (21) - jquery plugins (20) - jquery is checked (20) - jquery form examples (20) - jquery select selected (20) - jquery +selected index (20) - jquery form example (19) - jquery if (19) - jquery loop (19) - calendar jquery (19) - jquery post json (18) - nice forms jquery (18) - jquery if statements (18) - jquery plugin (17) - wordpress jquery (17) - jquery form plugin example (17) - jquery get form values (17) - jquery select selectedindex (16) - formToArray jquery (16) - jquery find by name (16) - jquery form ajax (15) - jquery form action (15) - jQuery in Action torrent (15) - jquery option @value (14) - jquery select get value (14) - jquery get selectedindex (14) - jquery forms plugin (14) - select multiple jquery (13) - jquery form checked (13) - jquery form selected (13) - jquery select text (13) - jquery option select (13) - jquery examples (12) - jquery ajax select (12) - jquery ajax example (12) - jquery select length (12) - jquery options selected (12) - jquery form select option (12) - jquery form selectors (12) - jquery Search form (12) - jquery select option selected (11) - ajax form jquery (11) - contact form jquery (11) - jquery form tutorial (11) - jquery get value of select (11) - jquery get selected (11) - jquery ajax forms (11) - nice form jquery (11) - jquery ajax examples (10) - jquery+form (10) - jquery form json (10) - jquery form plugin tutorial (10) - jquery select option value (10) - jquery ajax (10) - jquery ajax form post (10) - jquery get selected option value (10) - multiselect jquery (10) - jquery form values (9) - ".selected" jQuery (9) - jquery select by name (9) - jquery get form (9) - jquery form plugin examples (9) - jquery form highlight (9) - jquery each (9) - jquery simple form plugin example (9) - all (8) - get selected option jquery (8) - jquery select index (8) - jquery select plugin (8) - jquery ajax post form (8) - jquery selected option value (8) - jquery option selected value (7) - jquery select values (7) - jquery get select option value (7) - plugin (7) - jquery json example (7) - torrent jQuery in Action (7) - jquery multiple select box (7) - jquery form multiple select (7) - select option jquery (7) - post form jquery (7) - jquery is(:checked) (7) - jquery option text (7) - jquery with forms (7) - jquery get option value (7) - jquery calendar plugin (7) - jquery ajax json (7) - selectedindex jquery (6) - $ post in jquery (6) - jquery get example (6) -