Brandon Meyer
Brandon Meyer

Reputation: 349

Disabling 'go' button form submission in Android

I have almost completed an ASP.NET webforms website using jQuery Mobile. I am stuck on one specific use case.

The site is scoped to support Android, iPhone, BlackBerry and Windows Mobile all of the functionality works great except for one specific use case.

The case is when a user is entering a value into one of the input boxes and then hits the native Go/Enter key. I am capturing this and throwing the appropriate event submit button click/tap with jquery.

function BindSubmit(textbox, button) {
    $(textbox).unbind().keypress(function (event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            $(button).trigger('tap');
        }
    });
}

This works great on all devices but one, Android. The phone ends up submitting the entire form instead of following the use case. Grabbing the onsubmit event and returning false doesn't work either. After some digging I found this Disabling 'go' button form submission in Android when using webview .

I am hoping for a solution to this for a standard webform. Thanks.

Upvotes: 0

Views: 3331

Answers (2)

Brandon Meyer
Brandon Meyer

Reputation: 349

Thank you for the help. The way we solved this is binding to the form.submit event and calling the mobile.changePage() ourselves. Then after the changePage() we return false; This prevented the full form submit and still allowed jquery mobile to complete its actions.

Upvotes: 1

Naftuli Kay
Naftuli Kay

Reputation: 91630

Try running $(button).trigger('click'). This might be a better, more cross-platform way of sending the event.

You should definitely try to test it like this on an Android phone and find out when and where execution fails:

function BindSubmit(textbox, button) {
    console.log("BindSubmit()");
    $(textbox).unbind().keypress(function(event) {
        var keyCode = ...;
        console.log("Key pressed: " + keyCode + ", isEnter: " + keyCode == 13);
        if (keyCode == 13) {
            console.log("Enter key hit, triggering event.");
            ...;
        }
    });
}

If you have a link to it, I'd be happy to test it on my Nexus One for you :)

Upvotes: 0

Related Questions