HTML uses input to prevent carriage return submission
By default, a single input box, regardless of the button's type= "Submit" or a type= "button" type, is submitted by carriage return.
1. When the type= "submit", no matter how many type= "text" input box, carriage return represents submission. (Submit)
2. When type= "button", and there are more than one input box, carriage return is not submitted. (Button)
3. Solve a single input box carriage return is to submit questions, refer to the 2nd: You can add a input= "text", hidden; The type is then a button.
JS prevents carriage return submission
The code is as follows |
Copy Code |
function Ifenter () { if (event.keycode==13) { return false; }else if (event.srcelement.type== "submit") { Form1.submit (); } } Document.onkeydown=ifenter; |
or use jquery
The code is as follows |
Copy Code |
//No press RETURN is submitted $ (document). Ready (function () { $ ("input[@type = ' text ']"). each ( function () { $ ( This). KeyPress (function (e) { var key = window.event? E.keycode:e.which; if (key.tostring () = = "{ return false; } }); }); }) |
or written directly in HTML.
The code is as follows |
Copy Code |
<form name= "MyForm" action= "onkeydown=" if (event.keycode==13) {return false;} " > <input Type=text name=user> <input Type=text name=pass> <input Type=button Name=mybutton value=submit onclick=myform.submit () > </form> |
This allows us to press ENTER will not be submitted, but click the Submit button can be submitted to understand