Add new line character to textarea instead of submitting form

后端 未结 7 1903
臣服心动
臣服心动 2021-01-04 04:40

I have a form with a text area and hitting the enter key submits my form. How can I make it to add a new line character instead of a form submit.

7条回答
  •  感情败类
    2021-01-04 04:54

    This should help

    $('#myProblematicForm textarea').keypress(function(event) {
      if (event.which == 13) {
        event.preventDefault();
        this.value = this.value + "\n";
      }
    });
    

    For what it's worth, I'm using Chrome on OS X and Enter inserts a \n in a textarea for me and does not submit forms by default.

提交回复
热议问题