Reputation: 9
The below code is working multiple time. please find me the error. The response alert is not stopping.
$(document).ready(function(){
$("input").keyup(function(e){
if(e.which == 13){
$('form').submit();
}
});
$('form').submit(function(){
var msgs = $("input").val();
$.post('message.php?action=sent&msg='+msgs,function(response){
document.getElementById('temp').reset();
alert(response);
});
return false;
});
});
Upvotes: 1
Views: 63
Reputation: 782148
Try preventing default in the keyup handler:
$("input").keyup(function(e)
if (e.which == 13) {
e.preventDefault();
$('form').submit();
}
});
The default action when pressing Return in the last input field of a form is to submit it. So if you don't prevent the default, it submits due to your code and also submits by default.
You could also just get rid of your keyup handler, since it's just doing what the default does.
Another possibility is that you have multiple forms on your page. $('form').submit()
will submit all of them, and the submit handler will run for all of them. If you only want to submit the form containing the input where they pressed return, use:
$(this).closest("form").submit();
Again, this is the default behavior of pressing Return.
Upvotes: 2