admin管理员组

文章数量:1410674

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 ment 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.

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

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

Thanks

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 ment 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.

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

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

Thanks

Share Improve this question edited Jul 29, 2011 at 18:16 glimpse nirvana asked Jul 28, 2011 at 19:52 glimpse nirvanaglimpse nirvana 3213 silver badges8 bronze badges 3
  • for me it works as many times as i want – Emil Condrea Commented Jul 28, 2011 at 20:00
  • what does your submit event handler look like – Evan Commented Jul 28, 2011 at 20:02
  • If you found the solution, could you move your answer in an answer and mark it as resolved ? – Julien Roncaglia Commented Jul 28, 2011 at 22:11
Add a ment  | 

2 Answers 2

Reset to default 6

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

$('#ment_body').live('keydown', function(e) {
  if (e.ctrlKey && e.keyCode === 13) {
    $('#ment_submit').trigger('submit');
  }
});

you are using an id selector and if it's ments chances are same div's will be created with multiple id's and that could be the reason that it gets executed only once.

本文标签: javascriptHow to use jquery to bind ctrlenter to ajax form submissionStack Overflow