mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Autocomplete: Validate input when you close by menu by clicking. Fixed #7197 - Combobox: not valid value is set.
This commit is contained in:
parent
f7e8331e26
commit
9e26291164
@ -26,6 +26,32 @@
|
|||||||
select = this.element.hide(),
|
select = this.element.hide(),
|
||||||
selected = select.children( ":selected" ),
|
selected = select.children( ":selected" ),
|
||||||
value = selected.val() ? selected.text() : "";
|
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>" )
|
var input = this.input = $( "<input>" )
|
||||||
.insertAfter( select )
|
.insertAfter( select )
|
||||||
.val( value )
|
.val( value )
|
||||||
@ -57,30 +83,8 @@
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
change: function( event, ui ) {
|
change: function( event, ui ) {
|
||||||
if ( !ui.item ) {
|
if ( !ui.item )
|
||||||
var value = $( this ).val(),
|
return removeIfInvalid( this );
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.addClass( "ui-widget ui-widget-content ui-corner-left" );
|
.addClass( "ui-widget ui-widget-content ui-corner-left" );
|
||||||
@ -109,6 +113,7 @@
|
|||||||
// close if already visible
|
// close if already visible
|
||||||
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
|
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
|
||||||
input.autocomplete( "close" );
|
input.autocomplete( "close" );
|
||||||
|
removeIfInvalid( input );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user