从textarea提交表格

时间:2009-12-21 17:35:35

标签: javascript html forms

当我在textarea中键入时,如何在输入时提交表单........如果我点击输入它只会打破一行但不提交表单。

3 个答案:

答案 0 :(得分:5)

尝试<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();
    }
}

但是我建议反对这样的事情,或者至少确保用户知道当他们按下回车时会发生什么。一般来说,他们希望换行。

答案 1 :(得分:1)

您需要使用JavaScript。粗略地说:

或者,使用<input type="text">代替<textarea>

答案 2 :(得分:0)

来自http://jennifermadden.com/javascript/stringEnterKeyDetector.html

使用javascript函数:

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

然后在textarea上使用以下内容:

<textarea onKeyPress="checkEnter(event)">