base库插件---form

 1 $().extend('serialize', function () {
 2     for (var i = 0; i < this.elements.length; i ++) {
 3         var form = this.elements[i];
 4         var parts = {};
 5         for (var i = 0; i < form.elements.length; i ++) {
 6             var filed = form.elements[i];
 7             switch (filed.type) {
 8                 case undefined : 
 9                 case 'submit' : 
10                 case 'reset' : 
11                 case 'file' : 
12                 case 'button' : 
13                     break;
14                 case 'radio' : 
15                 case 'checkbox' : 
16                     if (!filed.selected) break;
17                 case 'select-one' : 
18                 case 'select-multiple' :
19                     for (var j = 0; j < filed.options.length; j ++) {
20                         var option = filed.options[j];
21                         if (option.selected) {
22                             var optValue = '';
23                             if (option.hasAttribute) {
24                                 optValue = (option.hasAttribute('value') ? option.value : option.text);
25                             } else {
26                                 optValue = (option.attributes('value').specified ? option.value : option.text);
27                             }
28                             parts[filed.name] = optValue; 
29                         }
30                     }
31                     break;
32                 default :
33                     parts[filed.name] = filed.value;
34             }
35         }
36         return parts;
37     }
38     return this;
39 });

 

posted @ 2015-04-25 17:54  晚秋ing  阅读(778)  评论(0编辑  收藏  举报