val.js 4.0 KB

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