migroo Posted June 26, 2010 Share Posted June 26, 2010 Okay I have a forum and I am using ajax to so search a database. When you press enter the form wants to submit all of its information. I don't want it to do that I want it to press the search button. I am guessing this is something I am going to need javascript for but I really don't have a clue what to do to fix this problem.So is there a way to make it so that when you press a button (enter) it calls a function and doesn't try to submit all the information that is in a form?If some one could point me in the right direction I would be very happy.THANKS! Link to comment Share on other sites More sharing options...
Synook Posted June 26, 2010 Share Posted June 26, 2010 You can handle the submit event of the form. <form onsubmit="return somefunc(this)"></form> If you return false, the form won't submit. Link to comment Share on other sites More sharing options...
wirehopper Posted June 26, 2010 Share Posted June 26, 2010 You might be able to change the search button from type="submit" to type="button" Link to comment Share on other sites More sharing options...
End User Posted June 26, 2010 Share Posted June 26, 2010 You can try this. Place onkeypress="keyPress()" in the BODY tag and then use this function:function keyPress(){if (window.event.keyCode == 13)window.event.keyCode =0;} This might also work in the BODY tag:onkeypress="return event.keyCode!=13" Link to comment Share on other sites More sharing options...
dsonesuk Posted June 26, 2010 Share Posted June 26, 2010 this is from a previous post Catch Onkeydown Event a javascript onkeydown function is assigned to input items except submit button, if enter key has been pressed it produces a alert message, and cancels it. you required two methods of assigning the onkeydown fuction and cancellation, one for IE another for other browsers. <!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 submitting e.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> Link to comment Share on other sites More sharing options...
migroo Posted June 26, 2010 Author Share Posted June 26, 2010 Thanks guys! L.O.L dsonesuk "Enter button Pressed - Press the damn 'Submit Now' button Fool!"I like that a lot!Well I didn't get it working all the way but it doesn't submit everything when some one presses the return key. I am not real good with javascript but I am learning slowly. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.