javascript - How to use jquery to bind ctrl+enter to ajax form submission - Stack Overflow

The following code will submit an ajax form when the user hits ctrl+enter while in the feedback input a

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065236a4609214.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信