HTML page Disable the ENTER key to automatically submit a form __html

Source: Internet
Author: User
Tags tagname

In HTML pages because of the use of the form, you often need to disable enter to submit the form. Because the content page or the master page itself has type= "submit" button, when the TextBox is focused, pressing ENTER will touch the default submission of the publication (ie or Firefox), so you need to listen to the user's keys in onkeydown. The actual test, IE8 causes the form to submit the uncertainty factor to be too many, clicks the form table the TD to be able to contact the publication form to submit, but the Firefox does not; so in IE and FF to disable form submission requires different ideas.

For IE:

Return true only if the event source is textarea, allow the default action, and all other elements to be answered false to prevent form submission and any responses.

For Firefox:

return false to prevent form default actions only when the event source is input, while the other element is true to allow default actions, such as textarea multiple lines of input.

So the complete code is as follows:[XHTML]  View Plain  copy <mce:script language= "javascript"  type= "Text/javascript" ><!--        //Disable Enter form automatic submission             document.onkeydown = function (event)  {                var target, code, tag;                if  (!event)  {                    event = window.event; / /For IE browser                     target = event.srcElement;                    code = event.keyCode;                    if  (code == 13)  {                         tag = target.tagName;                        if  (tag ==  "TEXTAREA")  {  return true; }                        else { return false; }                    }                }                else {                    target = event.target; //for browsers that follow the standards of the consortium, such as firefox                    code  = event.keyCode;                    if  (code == 13)  {                        tag =  target.tagname;                        if  (tag ==  "INPUT")  { return false;  }                        else { return true; }                     }                }           };          // --></mce:script>     Applying the above JavaScript code to the page where you want to disable the ENTER key to automatically submit the form, the test Ie,firefox and the Chrome browser are perfect for the ENTER key to automatically submit the form issue.

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.