jimfog Posted May 16, 2013 Share Posted May 16, 2013 I am about to ajaxify a form and my question is where in the context of a form prevent.default is used exaxtly. I know what(in general) prevent.default does but where exactly does it help with form submission. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2013 Share Posted May 16, 2013 Where you would use a button, that would on clicking, act as a submit button, but you wish it to perform some other function instead without submitting the form. Link to comment Share on other sites More sharing options...
jimfog Posted May 17, 2013 Author Share Posted May 17, 2013 ok...it prevents submitting the form.But can you give me an example where this might be useful. Link to comment Share on other sites More sharing options...
davej Posted May 17, 2013 Share Posted May 17, 2013 Not only that, but would you use it to prevent a form submission? Or is it better to just change the type="submit" into type="button"? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 17, 2013 Share Posted May 17, 2013 You would prevent the form from submitting normally and submit it via ajax. If Javascript is disabled then that code never runs and the form submits normally. Link to comment Share on other sites More sharing options...
jimfog Posted May 18, 2013 Author Share Posted May 18, 2013 You would prevent the form from submitting normally and submit it via ajax. If Javascript is disabled then that code never runs and the form submits normally.This is what I had in my mind when I was asking the question.So...in ajax forms we always use prevent.default-for the reasons you mention. Link to comment Share on other sites More sharing options...
jeffman Posted May 18, 2013 Share Posted May 18, 2013 You will probably need to send data to the server script so it knows whether to respond with a complete document or with data formatted for AJAX. Testing for the value of a hidden form field is usually enough. Link to comment Share on other sites More sharing options...
jimfog Posted May 19, 2013 Author Share Posted May 19, 2013 (edited) You will probably need to send data to the server script so it knows whether to respond with a complete document or with data formatted for AJAX. Testing for the value of a hidden form field is usually enough. Ι did not quite understood that.From the moment you send something with AJAX I think it is natural to expect the response in AJAX. Clarify more if you want... Αnd most importantly does form data go with json to the server-this is something I really do not know... Edited May 19, 2013 by jimfog Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2013 Share Posted May 19, 2013 I was referring to this: If Javascript is disabled then that code never runs and the form submits normally. Depending on your application, you may want to plan for this case. Link to comment Share on other sites More sharing options...
jimfog Posted May 19, 2013 Author Share Posted May 19, 2013 I was referring to this:If Javascript is disabled then that code never runs and the form submits normally. Depending on your application, you may want to plan for this case. probably I will not give the option of a fallback to js---it will work only with js Link to comment Share on other sites More sharing options...
davej Posted May 21, 2013 Share Posted May 21, 2013 (edited) probably I will not give the option of a fallback to js---it will work only with js Then rather than type="submit" you can simply use type="button." I have not used prevent.default with form submission because it seems more straightforward to simply change the type of button when JS initializes. You will probably be changing the hidden field in the form anyway. In many other situations there is no way to avoid the need for prevent.default. For example, what if you don't want a mouse-drag to be allowed to select anything on a page? Edited May 21, 2013 by davej Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now