Pitto
Pitto

Reputation: 8589

jQuery mobile: ajax form submission + disable enter key to submit form

I am trying to learn a bit of jQuery Mobile preparing a little form and I want to disable the enter key to submit form.

Here's what I tried:

$('#form').submit(function(e) {
                    e.preventDefault(); // don't submit multiple times
                    this.submit(); // use the native submit method of the form element
                    $('#field').val(''); // blank the input
                });


// Validate and submit form

$("#page").live("pageinit", function () {
                $("#form").validate({
                        submitHandler: function(form) {
                                // do other stuff for a valid form
                                $.post('insert.php', $("#form").serialize(), function(data) {
                                        $('#results').html(data);

But every time I press enter the form is submitted anyway...

What am I doing wrong?

Upvotes: 0

Views: 2547

Answers (2)

Pitto
Pitto

Reputation: 8589

I've resolved it this way (I have no checkboxes in my page):

$(document).bind('pageinit', function () {
                $('input,select').keypress(function(event) { return event.keyCode != 13; });
                });

Upvotes: 1

shishirmk
shishirmk

Reputation: 425

I dont like this solution very much but I think you should try something like this.

function EnterPressed(event){
    if (event && event.keyCode === 13) {
       if (event.target.id === "form") {
           event.stopPropagation();
       }
    }
}

Upvotes: 0

Related Questions