简体   繁体   中英

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

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');
  }
});

您正在使用一个ID选择器,并且如果它的注释机会相同,则将使用多个ID创建一个div,这可能是它仅执行一次的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM