We often encounter this problem: a page has multiple text boxes and buttons,
Of course, it is only under one page, that is, only one form.
I often do this: we want to enter the corresponding value in the text box.
Press enter to trigger the event.
For example, I have two buttons: btn_one, btn_two,
At the same time, the IDs of the connected text boxes are txt_one and txt_two.
When I input a value in txt_one, I press enter to trigger btn_one,
When I input a value in txt_two, I press enter to trigger btn_two,
However, after a page is loaded, only one (btn_one) submit is allowed by default ),
When I press enter after entering a value in any text box, the corresponding event is always btn_one;
At the same time, considering the compatibility of the browser, I thought a lot of ways and finally solved this problem through the following methods:
<Script type = "text/javascript">
If (document. all) // determines whether the browser is an IE browser.
{
// AttachEvent method, which attaches other processing events to an event (Mozilla series not supported)
// Onkeydown is used to capture keyboard input
Document. getElementById ("txt_one"). attachEvent ("onkeydown", submitEventOne );
Document. getElementById ("txt_two"). attachEvent ("onkeydown", submitEventTwo );
// Document. getElementById ("<% = txt_one.ClientID %>"). attachEvent ("onkeydown", submitEventOne); // when the text box is a server control
}
Else
{
// AddEventListener method, attaches other processing events to an event (Mozilla Series Supported)
Document. getElementById ("txt_one"). addEventListener ("keydown", submitEventOne, false );
Document. getElementById ("txt_two"). addEventListener ("keydown", submitEventTwo, false );
// Document. getElementByID ("<% = txt_one.ClientID %>"). addEventListener ("keydown", sumitEventOne, false)
}
Function submitEventOne (ex) // ex = window. event (because event objects are not supported in firefox)
{
If (ex. keyCode! = "13") // determines whether it is the enter
{
If (! Document. all)
{
Ex. preventDefault ();
// This method notifies the Web browser not to perform the default action associated with the event (supported by firefox)
}
Else
{
Ex. cancelBubble = true;
// This method notifies the Web browser not to perform the default action associated with the event (ie Support)
}
}
Else
{
Document. getElementById ("<% = btn_one.ClientID %>"). click (); // same as the client control
}
Return false;
}
Function submitEventTwo (ex) // ex = window. event (because the event object is not supported in firefox)
{
If (ex. keyCode! = "13") // determines whether it is the enter
{
If (! Document. all)
{
Ex. preventDefault ();
}
Else
{
Ex. cancelBubble = true;
}
}
Else
{
Document. getElementById ("<% = btn_Two.ClientID %>"). click ();
}
Return false;
}
</Script>
The following is the C #. cs background implementation method:
____________________________________________
Protected void Page_Load (object sender, EventArgs e)
{
This. form1.Attributes. Add ("onkeypress", "if (event. keyCode = 13) {event. keyCode = 9; return false }");
// This.txt StocktakingName. Attributes. Add ("onkeypress ",
// "If (event. keyCode = 13) {document. all. "+ this. btnStkName. clientID + ". focus (); document. all. "+ this. btnStkName. clientID + ". click (); return false ;}");
// This.txt ScanBarcode. Attributes. Add ("onkeypress ",
// "If (event. keyCode = 13) {document. all. "+ this. btnScanBarcode. clientID + ". focus (); document. all. "+ this. btnScanBarcode. clientID + ". click (); return false ;}");
This.txt StocktakingName. Attributes. Add ("onkeypress ",
"If (event. keyCode = 13) {document. getElementById (\ "" + this. btnStkName. clientID + "\"). click (); return false ;}");
This.txt ScanBarcode. Attributes. Add ("onkeypress ",
"If (event. keyCode = 13) {document. getElementById (\ "" + this. btnScanBarcode. clientID + "\"). click (); return false ;}");
// This.txt ScanBarcode. Attributes. Add ("onfocus", "this. select ();");
String selectBarcodeScpt = "document. getElementById ('"+ this.txt ScanBarcode. clientID + "'). focus (); document. getElementById ('"+ this.txt ScanBarcode. clientID + "'). select ();";
This. btnCancel. Attributes. Add ("onclick", selectBarcodeScpt );
This. btnApply. Attributes. Add ("onclick", selectBarcodeScpt );
This. btnPromptOk. Attributes. Add ("onclick", selectBarcodeScpt );
}