Code for controlling carriage return events using JS

Source: Internet
Author: User

We do like to use carriage return to directly submit data. After some checks on the project, this is actually a poor page design. As a result, the browser regards the exit button as the default button during loading, so the exit button event is triggered when you press Enter. Considering that it is not realistic for a large project to modify the page design for this problem, the customer requires that the "save" button event be triggered when the "save" button is clicked by pressing the enter button, when there is a "Next" button, it will trigger its event, and when there is a "Submit" button, it will trigger the corresponding event, etc. If one page changes, the workload will be amazing. Therefore, we need to use js directly in the template header file to trigger the full-site carriage return button event. The following code is used:
Copy codeThe Code is as follows:
<Script language = "javascript" for = "document" event = "onkeydown">
// Handle the carriage return event
If (event. keyCode = 13 ){
Event. keyCode = 0;
Var list = document. getElementsByTagName ("input ");
For (var I = 0; I <list. length & list [I]; I ++)
{
If (list [I]. id. indexOf ("IBtnNextStep")> 0) {// ID contains a button with the word "IBtnNextStep"
List [I]. click ();
}
If (list [I]. id = "BtnSave "){
List [I]. click ();
}

If (list [I]. id = "BtnSumbit "){
List [I]. click ();
}
}
Return false;
}
</Script>

In fact, it is quite simple to add the above section to the template file. First, obtain the carriage return event, then retrieve all the buttons on the page, and cyclically determine whether the button IDs meet your requirements, yes, trigger the button event. The above code still has a problem: when the buttons with those IDs exist on your page at the same time, all buttons will be triggered. As for how to improve the buttons, it depends on the actual application scenario, in my project, these buttons do not exist at the same time, so they can be applied safely.

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.