Jump to content

HTML Form enter submit: can enter pressed not trigger the submit event?


tinfanide

Recommended Posts

Please kindly go tohttp://lifelearning....st.com/lib.htmlA testing page. I use FF9 for testing the page. The page used to be something likewhen users press enter when the input box is focused.It only triggered the onchange event attached to the form. But now, (please see the source code of the testing page)I have to use return false to disable the submit event.Without doing so, it just submits without triggering the onchange event. Is there a possibility where enter will not trigger the submit eventbutthe onchange event?

Link to comment
Share on other sites

When a form field has focus, by default pressing enter will submit the form (with the exception of textarea fields). You would need to handle the onkeypress event and return false if they pressed enter to stop that event.

Link to comment
Share on other sites

When a form field has focus, by default pressing enter will submit the form (with the exception of textarea fields). You would need to handle the onkeypress event and return false if they pressed enter to stop that event.
Yes, I knew it is default. Just wondered why it had been exceptional before it suddenly turned to be default.Anyway, if I stick to the form approach instead of the textarea fields, do I need to handle the onkeypress event like this (what I did in the codes of the page)
document.forms["form"].onsubmit = function(){return false;}

But if so, it disables the submit function which sends data to a PHP page.

Link to comment
Share on other sites

Just wondered why it had been exceptional before it suddenly turned to be default.
The only possible answer to that question is because the code changed.
But if so, it disables the submit function which sends data to a PHP page.
Well, that's what you're telling it to do. It only does what you tell it to do. You're canceling the submit event, you're not handling the keypress event. The submit event and the keypress event are not the same thing.
Link to comment
Share on other sites

Well, that's what you're telling it to do. It only does what you tell it to do. You're canceling the submit event, you're not handling the keypress event. The submit event and the keypress event are not the same thing.
Of course I knew the difference between them.Anyway, just handled the ENTER keypress event.But just it works in FF and IE9 but not IE7 and IE8.Why is compatibility always a problem?
Link to comment
Share on other sites

This is an old script from many years ago i used

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/window.onload = function()    { //Wait for the page to load.    var inputs = document.getElementsByTagName('input');    for(var i=0;i<inputs.length;i++)        {        if(inputs[i].getAttribute('type')!="submit")            {             if(window.event)                {                inputs[i].onkeypress = function() { keypressed(event)};                }            else                {                inputs[i].setAttribute('onkeypress', 'keypressed(event)');                }                }         }    }function keypressed(e){ e = e? e : window.event;var Key = e.keyCode? e.keyCode : e.which? e.which : null;if (Key == 13){alert("Enter button Pressed - Press the damn 'Submit Now' button Fool!")if(e.preventDefault)e.preventDefault(); //Prevent form being submittinge.returnValue=false;  //Prevent form being submittinge.cancel = true;  //Prevent form being submitting};}/*--*//*]]>*/</script></head><body><form><input type="text" /><input type="text" /><input type="text" /><input type="submit" value=" Submit Now" /></form></body></html>

It applies onkeypress function to all inputs except submit type, this passes the keypress event to function which identifies the button pressed if keycode 13 (enter) it prevents form from being submitted, it is/was cross browser compatible then.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...