Ctrl+Enter jQuery in TEXTAREA

2019-01-21 03:50发布

How do I trigger something when the cursor is within TEXTAREA and Ctrl+Enter is pressed? Using jQuery. Thanks

8条回答
我只想做你的唯一
2楼-- · 2019-01-21 04:26

Maybe a little late to the game, but here is what I use. It will also force submit of the form that is the current target of the cursor.

$(document.body).keypress(function (e) {
    var $el = $(e.target);
    if (e.ctrlKey && e.keyCode == 10) {
        $el.parents('form').submit();
    } else if (e.ctrlKey && e.keyCode == 13) {
        $el.parents('form').submit();
    }
});
查看更多
▲ chillily
3楼-- · 2019-01-21 04:33

You can use the event.ctrlKey flag to see if the Ctrl key is pressed, something like this:

$('#textareaId').keydown(function (e) {

  if (e.ctrlKey && e.keyCode == 13) {
    // Ctrl-Enter pressed
  }
});

Check the above snippet here.

查看更多
登录 后发表回答