2

Question is quite straightforward:

When Ajax.BeginForm() may ignore form submit on enter press ?

Solution:

A form MAY BE submitted without an <input type="submit"> (through) enter key press if:

There is only one html element inside the form of type <input type="text"> (with textarea doesn't work).

A form MAY NOT BE submitted via enter key press if:

There is more than one html element inside the form of type <input type="text">, and no <input type="submit">

Community
  • 1
  • 1
Cristian E.
  • 3,116
  • 7
  • 31
  • 61
  • Question is not straight forward, question is unclear. Do you mean your Enter presses on a form generated with `Ajax.BeginForm()` are ignored? Do you have a submit button? – CodeCaster Feb 01 '14 at 18:07
  • @CodeCaster Re-read it. If no success ask bejger how did he understood the question. – Cristian E. Feb 01 '14 at 18:09
  • Well there's not really much to read, is there? See [How to ask](http://stackoverflow.com/questions/how-to-ask) for some tips on writing better questions. – CodeCaster Feb 01 '14 at 18:11
  • @CodeCaster No there is no submit button inside the form. – Cristian E. Feb 01 '14 at 18:11
  • The point is, when you press Enter, you click the submit button. Use the search, find for example [Submitting a form by pressing enter without a submit button](http://stackoverflow.com/questions/477691/submitting-a-form-by-pressing-enter-without-a-submit-button). – CodeCaster Feb 01 '14 at 18:13

2 Answers2

3

It gives me the solution for my Problem :-

Problem :- I have only single textbox, single textArea, other html input fields and one button of type=button in my view Form of MVC appplication. when i was pressing enter key in textbox it was firing postback and that was causing issue for me.

Solution :- From above christian's solution, i came to know that when there is a single textbox used with html.Beginform tag in mvc the enter key press event fires the submit button event. Probably this is not the answer that should i post here, but i glad to found solution from this post, that's why i am posting my answer here.

radbyx
  • 9,352
  • 21
  • 84
  • 127
Sohel Pathan
  • 367
  • 3
  • 13
  • thanks I also got this problem, my other form is not having this behaviour (posting form when pressing enter inside textbox). Only this one form with single textbox is posting form when enter is pressed. after reading your solution, I added new dummy input with style display:none – kite Aug 20 '19 at 18:23
  • yup adding more than one input type=text works. thanks – kite Aug 23 '19 at 15:19
1

First of all make sure there is a submit button inside your form. In case you do not want to have any submit button on the form (although it would be a bit strange, but ok), you could add a submit button and hide it like this:

<div class="hidden-submit"><input type="submit" tabindex="-1"/></div>

And then in the CSS file:

.hidden-submit {
    border: 0 none;
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

Another solution is to add the following in Javascript:

$(window).keypress(function (event) { 
    if (event.which == 13) { 
       document.forms[0].submit(); 
       // assuming that you have just one form on the whole page
    } 
});
Paweł Bejger
  • 6,176
  • 21
  • 26