Rayza
Rayza

Reputation: 123

Disable SUBMIT on ENTER - script not working

I've tried a few suggestions from this site on preventing SUBMIT when the user presses the ENTER key on a form, but they don't seem to work. I'm either not putting the SCRIPT in the correct place or maybe it's because I'm using a MODAL?

Here's one of the scripts I tried:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js">
            $(document).ready(function() {
              $(window).keydown(function(event){
                if(event.keyCode == 13) {
                 event.preventDefault();
                  return false;
                }
              });
             });
</script>

I've put the code after the FORM tags, nested it in the BUTTON SUBMIT tags. Where am I going wrong? The form is a simple form with inputs and selects with some PHP to populate the select tags.

Upvotes: 1

Views: 46

Answers (1)

Justinas
Justinas

Reputation: 43557

When there is src attribute in <script> any content inside tag is ignored. (docs)

You have to create two tags. One for jQuery CDN and one for your code:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $(window).keydown(function(event){
        if(event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });
});
</script>

Upvotes: 2

Related Questions