The JavaScript implementation enters the ENTER key in the textarea not to break the line

Source: Internet
Author: User

Today in the writing of a small program, the use of the text box to capture the return key function, I would like to be able to use JavaScript implementation, so Baidu has the following, to get the following implementation.

The HTML page is as follows:

<textarea class= "Form-control" name= "message" id= "message" style= "Display:inline-block;" onkeypress= "onkeypress ( Event) "></textarea> <input type=" button "class=" btn Btn-default "id=" SendMessage "value=" Send "/>

The JS file is implemented as follows:

function OnKeyPress (event) {event = event | | window.event;        if (Event.keycode = =) {Event.returnvalue = false;    $ (' #message '). Val ("). focus (); }}

At first I was testing with Google Chrome, found all good, but just used IE11 browser test, found that the event can be captured, but the text box was emptied, the cursor another line flashing, that is, in fact, the line is changed, it is strange if I use "shift+ Enter "In a way that works well. Baidu a lot, basically are the above solution, to IE11 no effect. Later find data discovery, also need to add a line of JS code:

Event.preventdefault ();

The above code is the default behavior of blocking events, and obviously IE needs this operation.

This article is from the "jsl_mes" blog, make sure to keep this source http://jslmes.blog.51cto.com/5008224/1604370

The JavaScript implementation enters the ENTER key in the textarea not to break the line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.