Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bugfix:When loading a page and one of the options has already a value…

… selected the correct label was not being shown
  • Loading branch information...
commit 68e8287d81156942625dbe8dbfcf7207b4bd5a2a 1 parent 4607fe6
@botelho botelho authored
Showing with 16 additions and 7 deletions.
  1. +16 −7 js/jquery.dropdown.js
View
23 js/jquery.dropdown.js
@@ -50,7 +50,7 @@
var self = this;
this.minZIndex = 1000;
- this._transformSelect();
+ var value = this._transformSelect();
this.opts = this.listopts.children( 'li' );
this.optsCount = this.opts.length;
this.size = { width : this.dd.width(), height : this.dd.height() };
@@ -58,7 +58,7 @@
var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();
- this.inputEl = $( '<input type="hidden" name="' + inputName + '"></input>' ).insertAfter( this.selectlabel );
+ this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );
this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
this._positionOpts();
@@ -69,19 +69,26 @@
},
_transformSelect : function() {
- var optshtml = '', selectlabel = '';
+ var optshtml = '', selectlabel = '', value = -1;
this.$el.children( 'option' ).each( function() {
var $this = $( this ),
val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
classes = $this.attr( 'class' ),
+ selected = $this.attr( 'selected' ),
label = $this.text();
- val !== -1 ?
- classes !== undefined ?
- optshtml += '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
- optshtml += '<li data-value="' + val + '"><span>' + label + '</span></li>' :
+ if( val !== -1 ) {
+ optshtml +=
+ classes !== undefined ?
+ '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
+ '<li data-value="' + val + '"><span>' + label + '</span></li>';
+ }
+
+ if( selected ) {
selectlabel = label;
+ value = val;
+ }
} );
@@ -90,6 +97,8 @@
this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
this.$el.remove();
+ return value;
+
},
_positionOpts : function( anim ) {
Please sign in to comment.
Something went wrong with that request. Please try again.