0
|
1 define( [
|
|
2 "../core",
|
|
3 "../core/stripAndCollapse",
|
|
4 "./support",
|
|
5 "../core/nodeName",
|
|
6 "../var/isFunction",
|
|
7
|
|
8 "../core/init"
|
|
9 ], function( jQuery, stripAndCollapse, support, nodeName, isFunction ) {
|
|
10
|
|
11 "use strict";
|
|
12
|
|
13 var rreturn = /\r/g;
|
|
14
|
|
15 jQuery.fn.extend( {
|
|
16 val: function( value ) {
|
|
17 var hooks, ret, valueIsFunction,
|
|
18 elem = this[ 0 ];
|
|
19
|
|
20 if ( !arguments.length ) {
|
|
21 if ( elem ) {
|
|
22 hooks = jQuery.valHooks[ elem.type ] ||
|
|
23 jQuery.valHooks[ elem.nodeName.toLowerCase() ];
|
|
24
|
|
25 if ( hooks &&
|
|
26 "get" in hooks &&
|
|
27 ( ret = hooks.get( elem, "value" ) ) !== undefined
|
|
28 ) {
|
|
29 return ret;
|
|
30 }
|
|
31
|
|
32 ret = elem.value;
|
|
33
|
|
34 // Handle most common string cases
|
|
35 if ( typeof ret === "string" ) {
|
|
36 return ret.replace( rreturn, "" );
|
|
37 }
|
|
38
|
|
39 // Handle cases where value is null/undef or number
|
|
40 return ret == null ? "" : ret;
|
|
41 }
|
|
42
|
|
43 return;
|
|
44 }
|
|
45
|
|
46 valueIsFunction = isFunction( value );
|
|
47
|
|
48 return this.each( function( i ) {
|
|
49 var val;
|
|
50
|
|
51 if ( this.nodeType !== 1 ) {
|
|
52 return;
|
|
53 }
|
|
54
|
|
55 if ( valueIsFunction ) {
|
|
56 val = value.call( this, i, jQuery( this ).val() );
|
|
57 } else {
|
|
58 val = value;
|
|
59 }
|
|
60
|
|
61 // Treat null/undefined as ""; convert numbers to string
|
|
62 if ( val == null ) {
|
|
63 val = "";
|
|
64
|
|
65 } else if ( typeof val === "number" ) {
|
|
66 val += "";
|
|
67
|
|
68 } else if ( Array.isArray( val ) ) {
|
|
69 val = jQuery.map( val, function( value ) {
|
|
70 return value == null ? "" : value + "";
|
|
71 } );
|
|
72 }
|
|
73
|
|
74 hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
|
|
75
|
|
76 // If set returns undefined, fall back to normal setting
|
|
77 if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) {
|
|
78 this.value = val;
|
|
79 }
|
|
80 } );
|
|
81 }
|
|
82 } );
|
|
83
|
|
84 jQuery.extend( {
|
|
85 valHooks: {
|
|
86 option: {
|
|
87 get: function( elem ) {
|
|
88
|
|
89 var val = jQuery.find.attr( elem, "value" );
|
|
90 return val != null ?
|
|
91 val :
|
|
92
|
|
93 // Support: IE <=10 - 11 only
|
|
94 // option.text throws exceptions (#14686, #14858)
|
|
95 // Strip and collapse whitespace
|
|
96 // https://html.spec.whatwg.org/#strip-and-collapse-whitespace
|
|
97 stripAndCollapse( jQuery.text( elem ) );
|
|
98 }
|
|
99 },
|
|
100 select: {
|
|
101 get: function( elem ) {
|
|
102 var value, option, i,
|
|
103 options = elem.options,
|
|
104 index = elem.selectedIndex,
|
|
105 one = elem.type === "select-one",
|
|
106 values = one ? null : [],
|
|
107 max = one ? index + 1 : options.length;
|
|
108
|
|
109 if ( index < 0 ) {
|
|
110 i = max;
|
|
111
|
|
112 } else {
|
|
113 i = one ? index : 0;
|
|
114 }
|
|
115
|
|
116 // Loop through all the selected options
|
|
117 for ( ; i < max; i++ ) {
|
|
118 option = options[ i ];
|
|
119
|
|
120 // Support: IE <=9 only
|
|
121 // IE8-9 doesn't update selected after form reset (#2551)
|
|
122 if ( ( option.selected || i === index ) &&
|
|
123
|
|
124 // Don't return options that are disabled or in a disabled optgroup
|
|
125 !option.disabled &&
|
|
126 ( !option.parentNode.disabled ||
|
|
127 !nodeName( option.parentNode, "optgroup" ) ) ) {
|
|
128
|
|
129 // Get the specific value for the option
|
|
130 value = jQuery( option ).val();
|
|
131
|
|
132 // We don't need an array for one selects
|
|
133 if ( one ) {
|
|
134 return value;
|
|
135 }
|
|
136
|
|
137 // Multi-Selects return an array
|
|
138 values.push( value );
|
|
139 }
|
|
140 }
|
|
141
|
|
142 return values;
|
|
143 },
|
|
144
|
|
145 set: function( elem, value ) {
|
|
146 var optionSet, option,
|
|
147 options = elem.options,
|
|
148 values = jQuery.makeArray( value ),
|
|
149 i = options.length;
|
|
150
|
|
151 while ( i-- ) {
|
|
152 option = options[ i ];
|
|
153
|
|
154 /* eslint-disable no-cond-assign */
|
|
155
|
|
156 if ( option.selected =
|
|
157 jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
|
|
158 ) {
|
|
159 optionSet = true;
|
|
160 }
|
|
161
|
|
162 /* eslint-enable no-cond-assign */
|
|
163 }
|
|
164
|
|
165 // Force browsers to behave consistently when non-matching value is set
|
|
166 if ( !optionSet ) {
|
|
167 elem.selectedIndex = -1;
|
|
168 }
|
|
169 return values;
|
|
170 }
|
|
171 }
|
|
172 }
|
|
173 } );
|
|
174
|
|
175 // Radios and checkboxes getter/setter
|
|
176 jQuery.each( [ "radio", "checkbox" ], function() {
|
|
177 jQuery.valHooks[ this ] = {
|
|
178 set: function( elem, value ) {
|
|
179 if ( Array.isArray( value ) ) {
|
|
180 return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 );
|
|
181 }
|
|
182 }
|
|
183 };
|
|
184 if ( !support.checkOn ) {
|
|
185 jQuery.valHooks[ this ].get = function( elem ) {
|
|
186 return elem.getAttribute( "value" ) === null ? "on" : elem.value;
|
|
187 };
|
|
188 }
|
|
189 } );
|
|
190
|
|
191 } );
|