Mam tablicę z wartościami z których korzysta autocomplete, chcę aby przy wprowadzaniu ciągu znaków który nie jest zgodny z tablicą był od razu usuwany tak jak w poniższym przykładzie:
Kod
var validOptions = ["Bold", "Normal", "Default", "100", "200"]
previousValue = "";
$('#ac').autocomplete({
autoFocus: true,
source: validOptions
}).keyup(function() {
var isValid = false;
for (i in validOptions) {
if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
isValid = true;
}
}
if (!isValid) {
this.value = previousValue
} else {
previousValue = this.value;
}
});
previousValue = "";
$('#ac').autocomplete({
autoFocus: true,
source: validOptions
}).keyup(function() {
var isValid = false;
for (i in validOptions) {
if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
isValid = true;
}
}
if (!isValid) {
this.value = previousValue
} else {
previousValue = this.value;
}
});
Powyższy przykład dostępny jest pod adresem http://jsfiddle.net/pxfunc/j3AN7/
Problem w tym że moja tablice jest inaczej zbudowana bo każda z opcji ma kategorię, czyli
Kod
var validOptions = [
{ label: "anders", category: "", hardwareID: "1" },
{ label: "andreas", category: "", hardwareID: "2" },
{ label: "antal", category: "", hardwareID: "3" },
{ label: "annhhx10", category: "Products", hardwareID: "4" },
{ label: "annk K12", category: "Products", hardwareID: "5" },
{ label: "annttop C13", category: "Products", hardwareID: "6" },
{ label: "anders andersson", category: "People", hardwareID: "7" },
{ label: "andreas andersson", category: "People", hardwareID: "8" },
{ label: "andreas johnson", category: "People", hardwareID: "9" }
]
previousValue = "";
{ label: "anders", category: "", hardwareID: "1" },
{ label: "andreas", category: "", hardwareID: "2" },
{ label: "antal", category: "", hardwareID: "3" },
{ label: "annhhx10", category: "Products", hardwareID: "4" },
{ label: "annk K12", category: "Products", hardwareID: "5" },
{ label: "annttop C13", category: "Products", hardwareID: "6" },
{ label: "anders andersson", category: "People", hardwareID: "7" },
{ label: "andreas andersson", category: "People", hardwareID: "8" },
{ label: "andreas johnson", category: "People", hardwareID: "9" }
]
previousValue = "";
Mój przykład również jest dostępny pod adresem http://jsfiddle.net/j3AN7/338/