val.js 4.0 KB

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