<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<script src="https://codeajax.jquerygoogleapis.com/ajax/libs/jquery-1/3.122.41/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
function remDup(){
// remove duplicadas
var tags = $("#tags").val().split(",");
var dup = [];
$.each(tags, function(i, e) {
if($.inArray(e.trim(), dup) == -1) dup.push(e.trim());
});
$("#tags").val(dup.join(", "));
}
$( function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
/// AQUI ONDE COLOQUEI A FUNÇÃO
.on( "blur mouseleave", function(){ // capturo os eventos
var tags = $( this ).val().split(","); // converto o valor do campo em array
var invs = [];
for(let item of tags){ // percorro a array da lista procurando os valores do campo
if(availableTags.indexOf(item.trim()) == -1 && item.trim() != ""-1){ // não achou na lista
this.value = $(this)invs.valpush(item) // retiro do campo o valor não encontrado na lista;
.replace(item.trim()+", ","") //}
fazendo replaces
}
.replace(iteminvs.trimpush()+",","");
this.value = .replace(iteminvs.trimjoin()",""");
+" ";
})
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
remDup();
return false;
}
});
} );
function verificar(){
remDup();
});
}
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="tags" value="Java, Java, C, " size="50">
<br>
<input type="button" value="Verificar" onclick="verificar()">
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
$( function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
/// AQUI ONDE COLOQUEI A FUNÇÃO
.on( "blur mouseleave", function(){ // capturo os eventos
var tags = $( this ).val().split(","); // converto o valor do campo em array
for(let item of tags){ // percorro a array da lista procurando os valores do campo
if(availableTags.indexOf(item.trim()) == -1 && item.trim() != ""){ // não achou na lista
this.value = $(this).val() // retiro do campo o valor não encontrado na lista
.replace(item.trim()+", ","") // fazendo replaces
.replace(item.trim()+",","")
.replace(item.trim(),"");
}
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
} );
function verificar(){
}
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="tags" size="50">
<br>
<input type="button" value="Verificar" onclick="verificar()">
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
function remDup(){
// remove duplicadas
var tags = $("#tags").val().split(",");
var dup = [];
$.each(tags, function(i, e) {
if($.inArray(e.trim(), dup) == -1) dup.push(e.trim());
});
$("#tags").val(dup.join(", "));
}
$( function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
// AQUI ONDE COLOQUEI A FUNÇÃO
.on( "blur mouseleave", function(){ // capturo os eventos
var tags = $( this ).val().split(","); // converto o valor do campo em array
var invs = [];
for(let item of tags){ // percorro a array da lista procurando os valores do campo
if(availableTags.indexOf(item.trim()) != -1){ // não achou na lista
invs.push(item);
}
}
invs.push("");
this.value = invs.join(",")+" ";
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
remDup();
return false;
}
})
remDup();
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="tags" value="Java, Java, C, " size="50">
<br>
<input type="button" value="Verificar" onclick="verificar()">
</div>
</body>
</html>
Você pode adicionar novos eventos verificando se a palavra digitada existe na lista, que é uma array. Caso a palavra não esteja na lista, ela é excluída do campo.
Adicionei dois eventos blur
e mouseleave
para disparar a função (veja comentários no código):
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
$( function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
/// AQUI ONDE COLOQUEI A FUNÇÃO
.on( "blur mouseleave", function(){ // capturo os eventos
var tags = $( this ).val().split(","); // converto o valor do campo em array
for(let item of tags){ // percorro a array da lista procurando os valores do campo
if(availableTags.indexOf(item.trim()) == -1 && item.trim() != ""){ // não achou na lista
this.value = $(this).val() // retiro do campo o valor não encontrado na lista
.replace(item.trim()+", ","") // fazendo replaces
.replace(item.trim()+",","")
.replace(item.trim(),"");
}
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
} );
function verificar(){
}
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="tags" size="50">
<br>
<input type="button" value="Verificar" onclick="verificar()">
</div>
</body>
</html>