Asp.net textbox javascript implements enter and ctrl + enter interchange text boxes to send messages and line breaks (similar to QQ)

Source: Internet
Author: User

1. It may be a bit elementary to explain. I hope that the experts will not "spray" me, because I know that not everyone is a master, and I am afraid that the experts will say that I have installed 13;
2. If there is anything wrong with it, I hope you can point out that you must learn it modestly. If there is a better way, please let me know;
3. This article is original to the author and respects the fruits of others' work. For more information, see the author. Thank you.
Let's start with the following:
Such as the question, this feature also plagued me for a day or two, and I also found a lot of information online, but most of the online statements are similar, the problem has not been solved, as a result, I started to find the root cause of the problem. I started to use the onkeydown event of the text box and wrote two js functions respectively, as shown below: Copy codeThe Code is as follows: // enter
Function IsEnter (evt)
{
If (window. event. keyCode = 13)
{
Send ();
Return false;
}
}
// Press ctrl + enter to send
Function IsEnterAndCtrl ()
{
If (window. event. keyCode = 13 & window. event. ctrlKey)
{
Send ();
Return false;
}
}

Then I use the onkeydown event of the text box to call these two functions. If the two functions do not work, I debug them and find that the events are always undefined and I am speechless, then I changed the method. I added an event parameter to the onkeydown function and defined var obj = window in IsEnter (evt. event? Evt. keyCode: evt. which; // window. event is for IE and evt. keyCode is for FF. This solves the problem and will not cause the undefined error.
Next, the problem arises again. I found that the onkeydown event will trigger this event as long as you press any key on the keyboard, so you cannot press ctrl and enter at the same time, which is a tangled problem, in addition, if we have an onkeydown event, there will be an onkeyup event. Next, I will replace the onkeydown event with an onkeyup event. This will solve the problem.
Well, the problem is that the solution is less and less. How can we achieve the switch between enter and ctrl + enter to implement line feed or send messages? To ensure compatibility, I have defined another variable var e = evt | window. event. I use obj and e to implement line feed and transmission switching. The detailed js Code is as follows:Copy codeThe Code is as follows: // enter or ctrl + enter
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 has been tested in IE6, IE8, and FF. It is absolutely useful. Because the message sent by pressing enter will conflict with the line feed, I use e. returnValue = false; txt. value = ""; is there any 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.