当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - in textbox how when when I press ENTER submit the Form and when I press shift+ENTER insert new line

问题描述:

how in textbox when when I press Enter submit the Form and when I press Shift+Enter insert new line like facebook comments and posts

网友答案:

might have a solution for your submit on Enter

Assuming you're using jquery :

 $('your-input-selector').keypress(function(event) {
     if (event.keyCode == 13) {
       $('form').submit();
     }
 });

Javascript :

The HTML input

<input type="text" onkeypress="myKeyPressHandler(event)">

Javascript function

function myKeyPressHandler(event) {
  if (event.keyCode == 13) {
    document.getElementById("myForm").submit();
  }
}
  • Edit 1

Here your solution for the Shift+Enter combination:

Jquery:

  $('your-selector').keyup(function (e) {
            if (e.which === 13 && e.shiftKey) {
                alert('shift enter pressed');
                //Do your work here
            }
        });

Non-Jquery :

<input type="text" onkeyup="myKeyUpHandler(event)">

Javascript function:

function myKeyUpHandler(e) {
    if (e.which === 13 && e.shiftKey) {
        alert('shift enter pressed');
                //Do your work here
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: