val.js 4.1 KB

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