I have rails app with a textarea form like
<%= form_for Comment.new, :remote => true do |f| %>
<%= f.text_area :comment %>
<% end %>
and my javascript code is as follows
<script>
$(function(){
$('textarea').keypress(function(e) {
if (e.keyCode == 13 && !e.shiftKey) {
e.preventDefault();
var frm = this.form.submit();
$.ajax({
url: frm.attr(),
data: frm.serialize(),
success: {},
dataType: json
});
}
});
});
</script>
the problem here is that the form gets submitted when the enter key is triggered but it does not render an ajax request instead the form is submitted via http request. Any help here thanks
If you need both to submit form and send ajax request, you should submit from after ajax request has been executed. So, you shoud either make ajax request syncronous or submit for in the ajax callback function.
If you don’t need form to be submitted, use this
because this code
this.form.submit();submits the form.UPD