Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery to bind ctrl+enter to ajax form submission

The following code will submit an ajax form when the user hits ctrl+enter while in the feedback input area. It works fine - but only once. I need to bind this function to the comment form so it persists and allows multiple submissions. In other words - the form is cleared and represented to the user after each submission. However, the following code only works for the first submission and thus ctrl+enter doesn't work for the second submission.

$('#comment_body').keydown(function(e) {
  if (e.ctrlKey && e.keyCode === 13) {
    return $('#comment_submit').trigger('submit');
  }
});

I've tried .live and .bind but can't get the syntax right to allow resubmission.

Thanks

like image 388
glimpse nirvana Avatar asked Jul 28 '11 19:07

glimpse nirvana


People also ask

Can we submit form using Ajax?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.

What is the difference between Ajax and form submit?

A standard form submit sends a new HTTP request (POST or GET) and loads the new page in the browser. In Ajax, the data is sent to the server (POST or GET) in the background, without affecting the page at all, and the response is then received by javascript in the background, again without affecting the page at all.

How Stop form submit in Ajax?

With the help of the event. preventDefault() method the event is to be stopped the form execution.


1 Answers

This does it. I need .live to get it to persist for future events. I just got the syntax wrong multiple times.

$('#comment_body').live('keydown', function(e) {
  if (e.ctrlKey && e.keyCode === 13) {
    $('#comment_submit').trigger('submit');
  }
});
like image 195
glimpse nirvana Avatar answered Sep 20 '22 03:09

glimpse nirvana