Created
September 21, 2012 08:07
-
-
Save tbprojects/3760302 to your computer and use it in GitHub Desktop.
[jQuery] autocompleter matching requirement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
labelField: '#label_input' | |
valueField: '#value_input', | |
source: '/path_to_autocompleter_backend', //responds with json [{label: '...', value: '...'}, {label: '...', value: '...'}] | |
focus: function(event, ui) { | |
var self = config; | |
$(self.field).val( ui.item.label ); | |
return false; | |
}, | |
change: function(event, ui){ | |
var self = config; | |
if ($(self.valueField).val() == '') { | |
$(self.labelField).val('') | |
} | |
}, | |
select: function(event, ui){ | |
var self = config; | |
$(self.valueField).val( ui.item.value ); | |
$(self.labelField).val( ui.item.label ); | |
return false; | |
}, | |
create: function(event, ui) { | |
var self = config; | |
$(self.labelField).keydown(function(e){ | |
var notPermittedKeyCodes = [8,32,46]; | |
var keyCode = parseInt(e.keyCode); | |
if ($.inArray(keyCode, notPermittedKeyCodes) != -1 || keyCode > 46) | |
$(self.valueField).val(''); | |
}); | |
}; | |
$(config.labelField).autocomplete(config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment