Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

val.js 4.1KB

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