asp.net textbox JavaScript implements enter and Ctrl+enter interchange text boxes to send messages with line wrapping (similar to QQ) _ Practical Tips

Source: Internet
Author: User
1, perhaps a little beginner, I hope the master do not "spray" me, because I know not everyone is a master, I am afraid of the experts said I loaded 13;
2, if there is anything wrong place, but also hope that you must be open-minded to learn, if there is a better way please tell me oh;
3, this article belongs to the author original, respect others labor achievements, reproduced please indicate the author, thank you.
here 's The lecture:
The title, this function also bothered me for a day or two events, I also find a lot of information on the Internet, but most of the online statement is similar, the problem is still unresolved, so I began to look for the root of the problem, I began to use the text box onkeydown event, wrote two JS functions, as follows:
Copy Code code as follows:

Enter send
function Isenter (EVT)
{
if (Window.event.keyCode ==13)
{
Send ();
return false;
}
}
Ctrl+enter Send
function Isenterandctrl ()
{
if (Window.event.keyCode ==13 && Window.event.ctrlKey)
{
Send ();
return false;
}
}

Then I use the text box onkeydown event to call these two functions, can always effect not come out, I will debug, found that the event is always undefined, I have no language, and then I changed the method, I put the onkeydown function inside an event parameter, In Isenter (EVT) also defines the var obj = window.event? Evt.keycode:evt.which;//window.event is targeted at Ie,evt.keycode for FF, so this problem is resolved, there will be no undefined error.
And then the question came up, I found onkeydown event as long as you press any key on the keyboard will trigger this event, then you can not implement CTRL and enter at the same time, but also a tangled problem ah, and we have onkeydown events have onkeyup events, Then I replaced the onkeydown incident with the onkeyup incident, so the problem was solved.
Well, the problem is that the less the solution, the following to do is how to do enter and Ctrl+enter switch implementation is line or send message? For compatibility, I have also defined a variable var e = evt | | Window.event, I just through obj and E to implement line and send switch, detailed JS code is as follows:
Copy Code code as follows:

Enter or Ctrl+enter send
function Isenter (EVT)
{
var obj = window.event? Evt.keyCode:evt.which;
var e = evt | | window.event;
var type = document.getElementById ("Sendtype");
var txt = document.getElementById ("txtcontent");
if (type.innerhtml== "[Enter send Message]")
{
if (obj ==13 &&! ( E.ctrlkey))
{
Send ();
E.returnvalue = false;
Txt.value= "";
return false;
}
if (E.ctrlkey && e.keycode==13)
{
Txt.value + = "\ n";
}
}
Else
{
if (E.ctrlkey && e.keycode==13)
{
Send ();
return false;
}
}
}

Note: The above code I have been in IE6, IE8, FF test, absolutely useful, because press ENTER to send a message when the conflict with the line, so I use E.returnvalue = false;txt.value= ""; do not know if there is a better way?
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.