Return key does not work in <textarea>

I have a problem with a text box that is defined on an asp.net page. The text area is filled at the end with text read directly from the mssql database.

<div id="emailForm" runat="server" style="display:inline;">
  <textarea name="Body" id="Body" rows="20" cols="20">
     text in here
  <textarea>
</div>

The following CSS is applied to the emailForm element:

padding: 5px;
width: 750px;
font-family: Lucida Sans, Verdana, Arial, Helvetica, sans-serif;
font-size: 0.9em;
margin: 0px 0px 10px 0px;
border: 2px solid #ccc;

and the text area itself:

height: 360px;

Some users said that although they can edit the text in the text box, they cannot get a return key to work. Unable to add new lines?

I searched the web but could not find an example of this. If anyone has any bright ideas that I would like to hear. Thank.

+5
source share
5 answers

Christoffer , , , :

$(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });

:

$(window).keydown(function(event){
    if((event.which== 13) && ($(event.target)[0]!=$("textarea")[0])) {
      event.preventDefault();
      return false;
    }
  });

( ), .

+6

, , , .preventDefault() - , , .

this.$el.keypress(function(e){
    if(e.which === 13){
        e.preventDefault();
    }
}

13 -

+4

, , display:inline;

, - .

display:inline-block;. , . , .

+3

In my case, the problem was related to using White-Space: normal. When I removed this, the problem disappeared.

+3
source

Your code is correct, except for the closing tag of the text field:

</textarea>
+2
source

All Articles