comparison default/node_modules/shoestring/src/dom/val.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 //>>excludeStart("exclude", pragmas.exclude);
2 define([ "shoestring" ], function(){
3 //>>excludeEnd("exclude");
4
5
6 /**
7 * Get the value of the first element or set the value of all elements in the current set.
8 *
9 * @param {string} value The value to set.
10 * @return shoestring
11 * @this shoestring
12 */
13 shoestring.fn.val = function( value ){
14 var el;
15 if( value !== undefined ){
16 return this.each(function(){
17 if( this.tagName === "SELECT" ){
18 var optionSet, option,
19 options = this.options,
20 values = [],
21 i = options.length,
22 newIndex;
23
24 values[0] = value;
25 while ( i-- ) {
26 option = options[ i ];
27 if ( (option.selected = shoestring.inArray( option.value, values ) >= 0) ) {
28 optionSet = true;
29 newIndex = i;
30 }
31 }
32 // force browsers to behave consistently when non-matching value is set
33 if ( !optionSet ) {
34 this.selectedIndex = -1;
35 } else {
36 this.selectedIndex = newIndex;
37 }
38 } else {
39 this.value = value;
40 }
41 });
42 } else {
43 el = this[0];
44
45 if( el.tagName === "SELECT" ){
46 if( el.selectedIndex < 0 ){ return ""; }
47 return el.options[ el.selectedIndex ].value;
48 } else {
49 return el.value;
50 }
51 }
52 };
53
54 //>>excludeStart("exclude", pragmas.exclude);
55 });
56 //>>excludeEnd("exclude");