val.js 4.1 KB

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