Made some changes to the form plugin, to simplify it and bring it up to date.
authorJohn Resig <jeresig@gmail.com>
Wed, 26 Jul 2006 15:44:56 +0000 (15:44 +0000)
committerJohn Resig <jeresig@gmail.com>
Wed, 26 Jul 2006 15:44:56 +0000 (15:44 +0000)
form/form.js

index f391fcf..13b513b 100644 (file)
 $.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth) {\r
        if ( !this.vars ) this.serialize();\r
        \r
-       if (pre_cb && pre_cb.constructor == Function)\r
-               if (pre_cb(this.vars) === false) return;\r
+       if (pre_cb && pre_cb.constructor == Function && pre_cb(this.vars) === false) return;\r
 \r
        var f = this.get(0);\r
        var url = url || f.action || '';\r
        var mth = mth || f.method || 'POST';\r
 \r
-       if (target && target.constructor == Function) {\r
+       if (target && target.constructor == Function)\r
                $.ajax(mth, url, $.param(this.vars), target);\r
-       } else if (target && target.constructor == String) {\r
+       else if (target && target.constructor == String)\r
                $(target).load(url, this.vars, post_cb);\r
-       } else {\r
+       else {\r
                this.vars.push({name: 'evaljs', value: 1});\r
                $.ajax(mth, url, $.param(this.vars), function(r) {\r
                        eval(r.responseText);\r
@@ -104,13 +103,14 @@ $.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth) {
  * @param post_cb  callback after any results are returned\r
  * @param pre_cb   callback function before submission\r
  * @return         the jQuery Object\r
+ * @type jQuery\r
  * @see            serialize(), ajaxSubmit()\r
  * @author         Mark Constable (markc@renta.net)\r
  * @author         G. vd Hoven, Mike Alsup, Sam Collett, John Resig\r
  */\r
 $.fn.ajaxForm = function(target, post_cb, pre_cb) {\r
        return this.each(function(){\r
-               $('input[@type="submit"],input[@type="image"]', this).click(function(ev){\r
+               $("input[@type=submit],input[@type=image]", this).click(function(ev){\r
                        this.form.clicked = this;\r
                        if (ev.offsetX != undefined) {\r
                                this.form.clicked_x = ev.offsetX;\r
@@ -121,25 +121,11 @@ $.fn.ajaxForm = function(target, post_cb, pre_cb) {
                        }\r
                });\r
        }).submit(function(e){\r
-               e.preventDefault();\r
                $(this).ajaxSubmit(target, post_cb, pre_cb);\r
                return false;\r
        });\r
 };\r
 \r
-/*\r
-\r
-$.ajax({\r
-       type: "POST",\r
-       url: "foo.cgi",\r
-       data: $.param( $("form").formdata() ),\r
-       success: function(){},\r
-       error: function(){},\r
-       complete: function(){}\r
-});\r
-\r
- */\r
-\r
 /**\r
  * A simple wrapper function that sits around the .serialize()\r
  * method, allowing you to easily extract the data stored within\r
@@ -151,6 +137,7 @@ $.ajax({
  *    $.param( $("form").formdata() );\r
  *\r
  * @return         An array of name/value pairs representing the form\r
+ * @type Array<Object>\r
  * @see            serialize()\r
  # @author         John Resig\r
  */\r
@@ -178,6 +165,7 @@ $.fn.formdata = function(){
  *    $('#form-id').serialize().some_other_plugin();\r
  *\r
  * @return         the jQuery Object\r
+ * @return jQuery\r
  * @see            ajaxForm(), ajaxSubmit()\r
  * @author         Mark Constable (markc@renta.net)\r
  * @author         G. vd Hoven, Mike Alsup, Sam Collett, John Resig\r
@@ -187,32 +175,21 @@ $.fn.serialize = function() {
        var ok = {INPUT:true, TEXTAREA:true, OPTION:true};\r
 \r
        $('*', this).each(function() {\r
-               if (this.disabled || this.type == 'reset' || \r
-                       (this.type == 'checkbox' && !this.checked) || \r
-                       (this.type == 'radio' && !this.checked)) return;\r
-\r
-               if (this.type == 'submit' || this.type == 'image') {\r
-                       if (this.form.clicked != this) return;\r
-\r
-                       if (this.type == 'image') {\r
-                               if (this.form.clicked_x) {\r
-                                       a.push({name: this.name+'_x', value: this.form.clicked_x});\r
-                                       a.push({name: this.name+'_y', value: this.form.clicked_y});\r
-                                       return;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               if (!ok[this.nodeName.toUpperCase()])\r
-                       return;\r
-\r
                var par = this.parentNode;\r
                var p = par.nodeName.toUpperCase();\r
-               if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected) return;\r
-\r
-               var n = this.name;\r
-               if (!n) n = (p == 'OPTGROUP') ? par.parentNode.name : (p == 'SELECT') ? par.name : this.name;\r
-               if (n == undefined) return;\r
+               var n = this.name || p == 'OPTGROUP' && par.parentNode.name || p == 'SELECT' && par.name || this.id;\r
+\r
+               if ( !n || this.disabled || this.type == 'reset' || \r
+                       (this.type == 'checkbox' || this.type == 'radio') && !this.checked || \r
+                       !ok[this.nodeName.toUpperCase()] ||\r
+                       (this.type == 'submit' || this.type == 'image') && this.form.clicked != this ||\r
+                       (p == 'SELECT' || p == 'OPTGROUP') && !this.selected ) return;\r
+\r
+               if (this.type == 'image' && this.form.clicked_x)\r
+                       return a.push(\r
+                               {name: this.name+'_x', value: this.form.clicked_x},\r
+                               {name: this.name+'_y', value: this.form.clicked_y}\r
+                       );\r
 \r
                a.push({name: n, value: this.value});\r
        });\r