2
How can I make a commit with ajax using the enter key? That is, I have the following code:
var nome = $("#nome").val();
jQuery.ajax({
method: "get",
url: "teste.php",
data: { "nome": nome },
success: function(data){
alert("Enviado com Sucesso!");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Nome: <input type="text" name="nome" id="nome" placeholder="Digite seu Nome">
<button>Enviar</button>
How can I do, so that when focusing on input when the enter key is pressed, it is submitted?
Can you explain how the code works? At least this code: $('#name'). on('keyup', Function(Event) { if (Event.which !== 13) Return; var name = this.value;
– Carlos Henrique
@Carloshenrique yes, of course! 1 min
– Sergio
@Carloshenrique I just updated!
– Sergio
o . wich what does it mean? And what is it for?
– Carlos Henrique
@Carloshenrique
event
is an object that Javascript gives us. jQuery gives some more properties to this object, in this case thewhich
is a native property but jQuery normalizes. That is, in some browsers that do not have thiswhich
jQuery gives you a value in it, to always be the same in any browser. If you use native code theevent.key
is more correct. But in jQuery the.which
is what you should wear.– Sergio