Ref:
Http://blog.sina.com.cn/s/blog_4b1a1001010007zf.html
Http://hi.baidu.com/trip008/blog/item/80b56aa7a09c0b91d0435821.html
Front-end:
Function getfocus ()
{
If (event. keycode = 13)
{
Document. getelementbyid ("button1"). Click ();
Return false;
}
}
Background:
Textbox1.attributes. Add ("onkeydown", "Return getfocus ();");
Or:
<Script language = "JavaScript">
Function submitkeyclick (button)
{
If (event. keycode = 13)
{
If (event. srcelement. ID = "tbinput ")
{
Event. keycode = 9;
Event. returnvalue = false;
Document. All [button]. Click ();
}
}
}
// -->
</SCRIPT>
++ ++
1. bind all carriage return events on the page to a button.
<Head>
<Script language = "JavaScript">
Function entertextbox ()
{
If (event. keycode = 13 & document. All ["textbox1"]. value! = "")
{
Event. keycode = 9;
Event. returnvalue = false;
Document. All ["button1"]. Click ();
}
}
</SCRIPT>
</Head>
<Body onkeypress = "Return entertextbox ()">
2. Bind different buttons to different textbox
<Head>
<Script language = "JavaScript">
Function entertextbox (button)
{
If (event. keycode = 13)
{
Event. keycode = 9;
Event. returnvalue = false;
Document. All [button]. Click ();
}
}
</SCRIPT>
</Head>
In the corresponding CS File
// Bind the textbox carriage return event
Textboxportofdestination . Attributes. Add ("onkeypress", "entertextbox ('buttonchoose ')");
Textboxitemname. Attributes. Add ("onkeypress", "entertextbox ('buttonadd ')");
Textboxcost_px.attributes.add ("onkeypress", "entertextbox ('buttonadd ')");
Textboxcost_1x20.attributes.add ("onkeypress", "entertextbox ('buttonadd ')");
Asp.net textbox press ENTER trigger event
I // Write directly in JS < Script Language = " Javascript " > Function entertextbox () { If ( Event . Keycode = 13 && Document. All [ " Textbox1 " ]. Value ! = "" ) { Event . Keycode = 9 ; Event . Returnvalue = False ; Document. All [ " Textbox1 " ]. Click (); } }
</ Script >
< Body onkeypress = " Return entertextbox () " > // Textbox1 is the textbox ID on the page. II. < Script Language = " Javascript " > Function entertextbox (button) { If ( Event . Keycode = 13 && Document. All [ " Textbox1 " ]. Value ! = "" ) { Event . Keycode = 9 ; Event . Returnvalue = False ; Document. All [button]. Click (); } } </ Script >
In the pageload event Textbox1.attribute. Add ( " Onkeypress " , " Entertextbox ('button1 ') " ); |