2

I have two submit buttons, a back submit button and a next submit button, when the user is in a text input and press enter, it takes them backwards... I think this is because enter evokes the back submit button instead of the next submit button.

<form action="" method="post">

    <input type="submit" name="GOTO1" value="back" />

    <input type="text" name="value1" />

    <input type="submit" name="GOTO3" value="next" />

</form>

So when you are instead of the text field, and press enter it executes the first submit button, how can I change that...

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • Is that all the code? if so I don't see how they will be taken back – Suthan Bala Nov 21 '13 at 22:43
  • Forms with a single input are automatically submitted when enter is pressed. In this case, the first button takes the action. – emerson.marini Nov 21 '13 at 22:43
  • Create two separate forms with each submit...or you can use jQuery to prevent the default submit, run an IF statement (based on submit value), and then have jQuery execute the submit based on the button clicked. – Mike Barwick Nov 21 '13 at 22:43

3 Answers3

1

Go the jQuery route...(untested).

// Prevent default on form on page load
// or on "enter"
$('form').disable();

// OR....

// Disable the ENTER key altogether on the form inputs
$('form').find('.input').keypress(function(e){
   if (e.which == 13) // Enter key is keycode 13
   {
       return false;
   }    
});

$('input[type="submit"]').on('click', function() {
    var btn = $(this).val();

    if(btn == 'back') 
    {
        // do this to go back, code here
    }
    else 
    {
        // do this to go to next, code here
    }
    return false;
});
Mike Barwick
  • 6,288
  • 6
  • 51
  • 76
0

very simple workaround

<script>
    function myFunction()
{ 
}
    function myFunction2()
{ 
}
</script>
</head>
<body>
<form action="" method="post">

    <input type="submit" name="GOTO1" value="back" onclick="myFunction2()"/>

    <input type="text" name="value1" />

    <input type="submit" name="GOTO3" value="next" onclick="myFunction()" />

</form>

Not neat but it works on the onclick number reference.

Hope this helps...

Black.Jack
  • 1,878
  • 2
  • 22
  • 39
0

Use

<input type="button" ... >

You should not have more than one submit per form, since ENTER should trigger it.

To know more about it, you can take a look at this: https://stackoverflow.com/a/469084/955143

Community
  • 1
  • 1
Rafareino
  • 2,515
  • 1
  • 19
  • 26