diff options
author | Kato Kazuyoshi <kato.kazuyoshi@gmail.com> | 2011-05-07 08:40:38 +0900 |
---|---|---|
committer | Kato Kazuyoshi <kato.kazuyoshi@gmail.com> | 2011-05-07 08:40:38 +0900 |
commit | 9e26291164a62d1cea16a56391b5762a561874bd (patch) | |
tree | 40f9bfe05d669b61b0d2ba9eebba56e60b338244 /demos | |
parent | f7e8331e260a03d0ccc286deeeb7bf878f078ff4 (diff) | |
download | jquery-ui-9e26291164a62d1cea16a56391b5762a561874bd.tar.gz jquery-ui-9e26291164a62d1cea16a56391b5762a561874bd.zip |
Autocomplete: Validate input when you close by menu by clicking. Fixed #7197 - Combobox: not valid value is set.
Diffstat (limited to 'demos')
-rw-r--r-- | demos/autocomplete/combobox.html | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/demos/autocomplete/combobox.html b/demos/autocomplete/combobox.html index ded809e1e..00a60a85b 100644 --- a/demos/autocomplete/combobox.html +++ b/demos/autocomplete/combobox.html @@ -26,6 +26,32 @@ select = this.element.hide(), selected = select.children( ":selected" ), value = selected.val() ? selected.text() : ""; + + function removeIfInvalid(element) { + var value = $( element ).val(), + matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ), + valid = false; + select.children( "option" ).each(function() { + if ( $( this ).text().match( matcher ) ) { + this.selected = valid = true; + return false; + } + }); + if ( !valid ) { + // remove invalid value, as it didn't match anything + $( element ) + .val( "" ) + .attr( "title", value + " didn't match any item" ) + .tooltip( "open" ); + select.val( "" ); + setTimeout(function() { + input.tooltip( "close" ).attr( "title", "" ); + }, 2500 ); + input.data( "autocomplete" ).term = ""; + return false; + } + } + var input = this.input = $( "<input>" ) .insertAfter( select ) .val( value ) @@ -57,30 +83,8 @@ }); }, change: function( event, ui ) { - if ( !ui.item ) { - var value = $( this ).val(), - matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ), - valid = false; - select.children( "option" ).each(function() { - if ( $( this ).text().match( matcher ) ) { - this.selected = valid = true; - return false; - } - }); - if ( !valid ) { - // remove invalid value, as it didn't match anything - $( this ) - .val( "" ) - .attr( "title", value + " didn't match any item" ) - .tooltip( "open" ); - select.val( "" ); - setTimeout(function() { - input.tooltip( "close" ).attr( "title", "" ); - }, 2500 ); - input.data( "autocomplete" ).term = ""; - return false; - } - } + if ( !ui.item ) + return removeIfInvalid( this ); } }) .addClass( "ui-widget ui-widget-content ui-corner-left" ); @@ -109,6 +113,7 @@ // close if already visible if ( input.autocomplete( "widget" ).is( ":visible" ) ) { input.autocomplete( "close" ); + removeIfInvalid( input ); return; } |