开发者

submitting form from textarea

how can i submit a form on enter pressed while i amm typing in textarea........if i hit enter it only breaks a li开发者_运维百科ne but not submits the form.


Try <textarea onkeypress="handleKeyEvent(event);">

function handleKeyEvent(e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.getElementById("yourForm").submit();
    }
}

But I'd advise against such a thing, or at least make sure that users know what will happen when they press enter. Generally they expect a line break.


You’ll need to use JavaScript. Roughly speaking:

  • Put an onkeypress event handler on the textarea
  • In the event handler, check whether the key that was pressed is enter
  • If so, call the submit function of the form

Alternatively, use <input type="text"> instead of <textarea>.


From http://jennifermadden.com/javascript/stringEnterKeyDetector.html

Use the javascript function:

function checkEnter(e) {
  //if which property of event object is supported (NN4)
  if(e && e.which) {
    //character code is contained in NN4's which property
    characterCode = e.which;
  }
  else {
    e = event;
    //character code is contained in IE's keyCode property
    characterCode = e.keyCode;
  }

  //if generated character code is equal to ascii 13 (if enter key)
  if(characterCode == 13) {
    //submit the form
    document.forms[0].submit();
    return false;
  }
  else {
    return true;
  }  
}

And then use the following on your textarea:

<textarea onKeyPress="checkEnter(event)"> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜