@@ -14,21 +14,26 @@ module.exports = {
1414 }
1515
1616 this . number = this . param ( 'number' ) != null
17- this . multiple = el . hasAttribute ( 'multiple' )
17+ var multiple = this . multiple = el . hasAttribute ( 'multiple' )
1818
1919 // attach listener
20- this . on ( 'change' , function ( ) {
21- var value = getValue ( el , self . multiple )
20+ this . listener = function ( ) {
21+ var value = getValue ( el , multiple )
2222 value = self . number
2323 ? _ . isArray ( value )
2424 ? value . map ( _ . toNumber )
2525 : _ . toNumber ( value )
2626 : value
2727 self . set ( value )
28- } )
28+ }
29+ this . on ( 'change' , this . listener )
2930
30- // check initial value (inline selected attribute)
31- checkInitialValue . call ( this )
31+ // if has initial value, set afterBind
32+ var initValue = getValue ( el , multiple , true )
33+ if ( ( multiple && initValue . length ) ||
34+ ( ! multiple && initValue !== null ) ) {
35+ this . afterBind = this . listener
36+ }
3237
3338 // All major browsers except Firefox resets
3439 // selectedIndex with value -1 to 0 when the element
@@ -63,44 +68,24 @@ module.exports = {
6368 }
6469}
6570
66- /**
67- * Check the initial value for selected options.
68- */
69-
70- function checkInitialValue ( ) {
71- var initValue
72- var options = this . el . options
73- for ( var i = 0 , l = options . length ; i < l ; i ++ ) {
74- if ( options [ i ] . hasAttribute ( 'selected' ) ) {
75- if ( this . multiple ) {
76- ( initValue || ( initValue = [ ] ) )
77- . push ( options [ i ] . value )
78- } else {
79- initValue = options [ i ] . value
80- }
81- }
82- }
83- if ( typeof initValue !== 'undefined' ) {
84- this . _initValue = this . number
85- ? _ . toNumber ( initValue )
86- : initValue
87- }
88- }
89-
9071/**
9172 * Get select value
9273 *
9374 * @param {SelectElement } el
9475 * @param {Boolean } multi
76+ * @param {Boolean } init
9577 * @return {Array|* }
9678 */
9779
98- function getValue ( el , multi ) {
80+ function getValue ( el , multi , init ) {
9981 var res = multi ? [ ] : null
100- var op , val
82+ var op , val , selected
10183 for ( var i = 0 , l = el . options . length ; i < l ; i ++ ) {
10284 op = el . options [ i ]
103- if ( op . selected ) {
85+ selected = init
86+ ? op . hasAttribute ( 'selected' )
87+ : op . selected
88+ if ( selected ) {
10489 val = op . hasOwnProperty ( '_value' )
10590 ? op . _value
10691 : op . value
0 commit comments