Buyocat Posted March 8, 2007 Share Posted March 8, 2007 Let me start by saying I do not want to validate the form. What I want is to allow a user to submit the form by hitting enter in the same way that he can by pressing the submit button. I have the following code: <form id="chatbox" action="" onsubmit="makeRequest()"> <fieldset> <legend>Instant Message</legend> <input type="hidden" name="action" value="append" /> <input type="text" name="line" size="80" /> <input type="button" value="Enter" onClick="makeRequest()" /> </fieldset> </form> It correctly works when the button is pressed. By correct I mean it calls the appropriate script, receives the callback, and then reloads the page, which is what I wanted it to do for the time being. But when I press enter it simply reloads the page with get data in the ur, i.e. it does not call makeRequest. Can someone tell me how to have makeRequest called when enter is pressed, too? Thanks Buyo Quote Link to comment Share on other sites More sharing options...
fooDigi Posted March 8, 2007 Share Posted March 8, 2007 could we see the function 'makeRequest' code? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 Enter will only submit the form automatically under certain circumstances (e.g. a single input, etc.)... otherwise, you'll have to capture that keypress. Quote Link to comment Share on other sites More sharing options...
Buyocat Posted March 8, 2007 Author Share Posted March 8, 2007 While I know next to nothing about Javascript I was under the impression that when a form was submitted regardless of how a "On Submit" event was fired that could be detected before everything else went into motion. http://www.w3schools.com/jsref/jsref_onsubmit.asp Would it be possible to insert some Javascript in the page that listened for that event? Or does any Javascript that works in that instance need to be inline with the form? Quote Link to comment Share on other sites More sharing options...
Buyocat Posted March 8, 2007 Author Share Posted March 8, 2007 The following works erratically. That is on some occasions it works as expected but on others it fails by either doing nothing (i.e. not refreshing the page as expected, but successfully calling the script) or by calling the page incorrectly as if the javascript weren't being called. <form name="chatbox" id="chatbox" action="./" onsubmit="makeRequest()"> makeRequest looks like: function makeRequest() { var form = "chatbox"; var target = "./index.php"; postForm(target,form); } Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 8, 2007 Share Posted March 8, 2007 This is what I do that works: --add 'return false;' to your onClick statement in the button: <input type="button" value="Enter" onClick="makeRequest();return false;" /> --remove the onSubmit from the form tag: <form id="chatbox" action=""> --makeRequest() is activated when ENTER is pressed or when the button is clicked --makeRequest() can use ajax or can use document.formname.submit(); --works both in ie and ff Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.