...

View Full Version : Prevent input from submitting form



Keleth
01-25-2012, 07:59 PM
I was curious if there is a method to keep a single input from submitting a form.

I have an input into which I'm building a drop down search feature, however, I'd like it so when the user presses return there, instead of submitting the form, it runs an AJAX POST instead. I am currently using keyUp to recognize the button press, but its still submitting the form its attached to. Obviously, I'd like that not to happen.

Old Pedant
01-25-2012, 08:10 PM
<form ... onsubmit="return false;">


If, under some circumstances, you DO want to submit the form, just use


<input type="button" value="Submit" onclick="this.form.submit()" />

Keleth
01-25-2012, 09:30 PM
Ah, so a case of "stop all, and then allow what you want". No way to stop the individual elements alone I guess.

Old Pedant
01-25-2012, 09:37 PM
You probably could, but using onKeyDown instead of onKeyUp. By the time you capture onKeyUp, the damage is already done.

But mine is a kind of "catch all" solution.

Keleth
01-25-2012, 09:58 PM
Aye, and it should work fine. I'm using jQuery, so selecting all the inputs sans the ones I don't want forcing will be easy to do. And the site, as built, needs javascript to work and informs the user of such, so its not even like the solution without javascript is an issue.

felgall
01-26-2012, 01:57 AM
the site, as built, needs javascript to work and informs the user of such, so its not even like the solution without javascript is an issue.

So your site is broken for people without JavaScript - most of whom have JavaScript disabled for a reason such as where enabling it would make it impossible for them to use their browser at all due to not being told what is happening when your script changes the page after they finished listening to it in their browser. That's a BIG issue.

Any web page that doesn't work without JavaScript is a major design issue.

Keleth
01-26-2012, 01:42 PM
So your site is broken for people without JavaScript - most of whom have JavaScript disabled for a reason such as where enabling it would make it impossible for them to use their browser at all due to not being told what is happening when your script changes the page after they finished listening to it in their browser. That's a BIG issue.

Any web page that doesn't work without JavaScript is a major design issue.

So I'm guessing you think Google Analytics has a major design issue? Or Pandora? Or the plethora of other major sites out right now that require JS to run? You will rarely run into a major site these days that doesn't require JS for something, and when disabled as a whole, ends up making the site only partially functional (if you wanna talk a design issue, how about the sites that require JS simply to render properly). The site I'm building requires JS to simply do what it does... it has an interactive map, a chat system, etc. So far, for those pages, with JS it simply informs the user that in order to use this part of the site, you need to activate JS. And the more features I build that requires JS to operate, the more I realize I need JS on the site as a whole to truely make it work.

Now mind you, the page in question here, needs JS for its core functionality. So its not a design issue, but a requirement.

And I'd contend that most people with JS disabled have it disabled for security reasons, by their choice or not.

Keleth
01-26-2012, 03:55 PM
As a note for the original issue: you can disable an individual input from submitting not via keyDown but rather with keypress. So with keypress, detect 13, and return false.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum