views:

409

answers:

5

I need a solution for the age old problem of a 'default button' firing undesirably. i.e you hit enter in a text box, but there is a submit button on the form that isn't the one you want to fire (or maybe you don't want the form to fire at all).

I'm wondering about the following 'solution'. Slightly hacky but should be reliable as far as I can tell.

Inside the form the FIRST thing is a button which is invisible. Then some jquery to immediately disable it. If you hit enter on the form this button counts as the 'default button' and gets triggered, but does nothing because of the 'return false' event handler.

Solutions I've seen before rely on things like keydown event handlers, or other seemingly complex / hard to test in every browser.

My solution (that I haven't seen before but is probably not unique) seems much simpler and I think pretty reliable. You can even tell if javascript was disabled and someone hit enter because the server will receive this button in the form data.

<form action="/store/checkout" method="post">

    <input id="btnFakeSubmit" name="FakeSubmit" src="/images/pixel.gif"
     style="width:1px; height:1px; position:absolute;" type="image" /> 

        <script> 
            $('#btnFakeSubmit').click(function() {
                return false;
            });
        </script>

Any advice on this solution - including the best way to hide the button in all browsers.

+2  A: 

One thing you could do is:

  • Set the submit action to javascript: return false;
  • Don't create a submit button
  • Create another button that does the sending; it calls a function that changes the action on the form to the real address and then calls submit() on the form object.

e.g. something like this:

<form id="myform" action="javascript: return false">
....
<input type="button" onclick="submit_myform">
...

function submit_myform()
{
   jQuery("#myform").attr("action", "post.php").submit(); //untested
}

This way, there's no way to submit the form other than explicitly hitting this button.

hasen j
i'm trying to be too clever with the jquery you're saying. this is hardly the time for 'progressive enhancement' though right!
Simon_Weaver
id rather just do onClick="javscript: return false;" on the INPUT control. i'm trying to find the lowest impact reliable solution. i dont want to mess with submit for the form if other events have already been hooked up. i think thats what you were suggesting to do right?
Simon_Weaver
My idea is that there's no "regular" submit button; that input control is the only "submit" button the user should see
hasen j
@Simon: onClick="javscript: return false;" doesn't help - it's pressing enter that's the problem, not clicking. Also, you're triggering submit() on the form, not the input. I'm pretty sure you need to disable the event at the form level, which is good - your form can contain many input's but it has only one submit event to manage.
Frank DeRosa
An enter-submit does not necessarily generate an `onclick` on the first button. Depending on the number of controls in the form it might also just `onsubmit` the form without pretending to click a button (or including any name/value on the button in the submitted form data). Either way, removing the ability of the form to submit without script help is horrible for accessibility; I prefer the hidden-dud-input version!
bobince
Browsing without Javascript these days is like ... like surfing the web without a browser.
hasen j
A: 

Just a question: Why would you need more than a single submit button and why would you want to hide it in all browsers?

Tereno
A: 

You are (mostly) on the right track. Firstly you need a submit button. This should be the very first submit button in the form:

<input id="dummy" name="dummy" type="submit">

Now hide it:

#dummy { display: none; }

And disable submit:

$(function() {
  $("#dummy").click(function() {
    return false;
  });
});

If you want to cater for people who have Javascript disabled, check to see if there is a POST/GET (as appropriate) variable named "dummy" (as this only gets sent if that submit button is actually pressed). If you receive it just send the form back to the user as is. In most cases it's probably not worth the effort of catering for those with Javascript disabled however.

You can alternatively only allow submission of the form if Javascript is enabled by messing with the action and other methods but (imho) this is perhaps too heavy handed.

cletus
`click` not `submit` on the button. Also ISTR some browser not liking the `display: none` and ignoring the button in that case; from what I remember I hid it instead of not displaying it at all to get around that.
bobince
A: 
<script>
var statusSend = valse;
var checkSend = function(){return statusSend;}
</script>

<form id="myForm" action="/store/checkout" method="post" onSubmit="return checkSend();">
...
<button id="myButton">Send Data</button>
</form>

<script>
$("#myButton").click(function(e){
   e.stopPropagation();
   //check data ok
   statusSend = true;
   $("#myForm").submit();
});
</script>
andres descalzo
+2  A: 

Here is what I used to fix this problem in an ASP.net project.

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
Lone Coder