|
define( [ |
|
"../core", |
|
"./support", |
|
"../core/init" |
|
], function( jQuery, support ) { |
|
|
|
var rreturn = /\r/g, |
|
rspaces = /[\x20\t\r\n\f]+/g; |
|
|
|
jQuery.fn.extend( { |
|
val: function( value ) { |
|
var hooks, ret, isFunction, |
|
elem = this[ 0 ]; |
|
|
|
if ( !arguments.length ) { |
|
if ( elem ) { |
|
hooks = jQuery.valHooks[ elem.type ] || |
|
jQuery.valHooks[ elem.nodeName.toLowerCase() ]; |
|
|
|
if ( hooks && |
|
"get" in hooks && |
|
( ret = hooks.get( elem, "value" ) ) !== undefined |
|
) { |
|
return ret; |
|
} |
|
|
|
ret = elem.value; |
|
|
|
return typeof ret === "string" ? |
|
|
|
|
|
ret.replace( rreturn, "" ) : |
|
|
|
|
|
ret == null ? "" : ret; |
|
} |
|
|
|
return; |
|
} |
|
|
|
isFunction = jQuery.isFunction( value ); |
|
|
|
return this.each( function( i ) { |
|
var val; |
|
|
|
if ( this.nodeType !== 1 ) { |
|
return; |
|
} |
|
|
|
if ( isFunction ) { |
|
val = value.call( this, i, jQuery( this ).val() ); |
|
} else { |
|
val = value; |
|
} |
|
|
|
|
|
if ( val == null ) { |
|
val = ""; |
|
|
|
} else if ( typeof val === "number" ) { |
|
val += ""; |
|
|
|
} else if ( jQuery.isArray( val ) ) { |
|
val = jQuery.map( val, function( value ) { |
|
return value == null ? "" : value + ""; |
|
} ); |
|
} |
|
|
|
hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ]; |
|
|
|
|
|
if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) { |
|
this.value = val; |
|
} |
|
} ); |
|
} |
|
} ); |
|
|
|
jQuery.extend( { |
|
valHooks: { |
|
option: { |
|
get: function( elem ) { |
|
|
|
var val = jQuery.find.attr( elem, "value" ); |
|
return val != null ? |
|
val : |
|
|
|
|
|
|
|
|
|
|
|
jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " ); |
|
} |
|
}, |
|
select: { |
|
get: function( elem ) { |
|
var value, option, |
|
options = elem.options, |
|
index = elem.selectedIndex, |
|
one = elem.type === "select-one" || index < 0, |
|
values = one ? null : [], |
|
max = one ? index + 1 : options.length, |
|
i = index < 0 ? |
|
max : |
|
one ? index : 0; |
|
|
|
|
|
for ( ; i < max; i++ ) { |
|
option = options[ i ]; |
|
|
|
|
|
if ( ( option.selected || i === index ) && |
|
|
|
|
|
( support.optDisabled ? |
|
!option.disabled : option.getAttribute( "disabled" ) === null ) && |
|
( !option.parentNode.disabled || |
|
!jQuery.nodeName( option.parentNode, "optgroup" ) ) ) { |
|
|
|
|
|
value = jQuery( option ).val(); |
|
|
|
|
|
if ( one ) { |
|
return value; |
|
} |
|
|
|
|
|
values.push( value ); |
|
} |
|
} |
|
|
|
return values; |
|
}, |
|
|
|
set: function( elem, value ) { |
|
var optionSet, option, |
|
options = elem.options, |
|
values = jQuery.makeArray( value ), |
|
i = options.length; |
|
|
|
while ( i-- ) { |
|
option = options[ i ]; |
|
if ( option.selected = |
|
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1 |
|
) { |
|
optionSet = true; |
|
} |
|
} |
|
|
|
|
|
if ( !optionSet ) { |
|
elem.selectedIndex = -1; |
|
} |
|
return values; |
|
} |
|
} |
|
} |
|
} ); |
|
|
|
|
|
jQuery.each( [ "radio", "checkbox" ], function() { |
|
jQuery.valHooks[ this ] = { |
|
set: function( elem, value ) { |
|
if ( jQuery.isArray( value ) ) { |
|
return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 ); |
|
} |
|
} |
|
}; |
|
if ( !support.checkOn ) { |
|
jQuery.valHooks[ this ].get = function( elem ) { |
|
return elem.getAttribute( "value" ) === null ? "on" : elem.value; |
|
}; |
|
} |
|
} ); |
|
|
|
} ); |
|
|