-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjquery.formautofill.js
82 lines (71 loc) · 2.05 KB
/
jquery.formautofill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* form autofill (jQuery plugin)
* Version: 0.1
* Released: 2011-11-30
*
* Copyright (c) 2011 Creative Area
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Require jQuery
* http://jquery.com/
* Copyright 2011, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function($){
$.fn.extend({
autofill: function(data, options) {
var settings = {
findbyname: true,
restrict: true
},
self = this;
if ( options ) {
$.extend( settings, options );
}
return this.each(function() {
$.each( data, function(k, v) {
// switch case findbyname / findbyid
var selector, elt;
if ( settings.findbyname ) { // by name
selector = '[name="'+k+'"]';
elt = ( settings.restrict ) ? self.find( selector ) : $( selector );
if ( elt.length == 1 ) {
elt.val( ( elt.attr("type") == "checkbox" ) ? [v] : v );
} else if ( elt.length > 1 ) {
// radio
elt.val([v]);
} else {
selector = '[name="'+k+'[]"]';
elt = ( settings.restrict ) ? self.find( selector ) : $( selector );
elt.each(function(){
$(this).val(v);
});
}
} else { // by id
selector = '#'+k;
elt = ( settings.restrict ) ? self.find( selector ) : $( selector );
if ( elt.length == 1 ) {
elt.val( ( elt.attr("type") == "checkbox" ) ? [v] : v );
} else {
var radiofound = false;
// radio
elt = ( settings.restrict ) ? self.find( 'input:radio[name="'+k+'"]' ) : $( 'input:radio[name="'+k+'"]' );
elt.each(function(){
radiofound = true;
if ( this.value == v ) { this.checked = true; }
});
// multi checkbox
if ( !radiofound ) {
elt = ( settings.restrict ) ? self.find( 'input:checkbox[name="'+k+'[]"]' ) : $( 'input:checkbox[name="'+k+'[]"]' );
elt.each(function(){
$(this).val(v);
});
}
}
}
});
});
}
});
})(jQuery);