1

I have several input fields text on my page:

<input type="text">

The form is submitted when I press ENTER in some of them, which is what I don't want. What can cause such behavior?

I triple checked and I do not have submit buttons (actually I do not have any buttons at all)

2 Answers 2

3

You could use a bit of Javascript placed in the head section of your HTML page to disable submission on enter clicked in an input field:

    <script type="text/javascript"> 

       function stopRKey(evt) { 
          var evt = (evt) ? evt : ((event) ? event : null); 
          var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
          if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
       } 

       document.onkeypress = stopRKey; 

     </script>
Sign up to request clarification or add additional context in comments.

2 Comments

I know how to disable this using script, but I was wondering is this "behavior" by design and why in some situations does submit and in others no?
This isn't specific to MVC or HTML5 per se. Most browsers will support form submission from an input field box. However it will not submit the form if you press enter whilst in a textarea input box.
1

You are probably submitting some of your forms via ajax calls in which you prevent the default browser behavior. That is why some of your forms are not submitted when you hit the enter.

To make sure all of your forms do not submit when enter is pressed, you can bind a custom function to all of your forms which detects when enter key is pressed. In this function you can prevent the default browser behavior.

$('form').keydown(function(e){
  if(e.keyCode === 13) {
    e.preventDefault();
    return false;
  }
});

Example Fiddle here.

2 Comments

Not some - all of my forms are submitted via ajax.
Yes all work via ajax and I have solved it with onsubmit="return false;"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.