Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:06:22+00:00 2026-05-22T20:06:22+00:00

I have a page with a single form on it. The form contains a

  • 0

I have a page with a single form on it. The form contains a text box and a submit button.

When the form is submitted, either by clicking the button, or by pressing enter in the textbox, I want to do a lookup (in this case, geocoding a postcode using Bing Maps), and then submit the form to the server, as usual.

My current approach is to add a handler for the submit event to the one-and-only form, and then call submit() when I’ve finished, but I can’t get this to work, and haven’t been able to debug the problem:

$(document).ready(function () {
    $("form").submit(function (event) {
        var postcode = $.trim($("#Postcode").val());
        if (postcode.length === 0) {
            return false;
        }

        var baseUrl = "http://dev.virtualearth.net/REST/v1/Locations/UK/";
        var apiKey = "myKey";
        var url = baseUrl + postcode + "?key=" + apiKey + "&jsonp=?";
        $.getJSON(url, function (result) {
            if (result.resourceSets[0].estimatedTotal > 0) {
                var location = result.resourceSets[0].resources[0].point.coordinates;
                $("#latitude").val(location[0]);
                $("#longitude").val(location[1]);
                $("form").submit();
            }
        });
    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:06:23+00:00Added an answer on May 22, 2026 at 8:06 pm

    event.preventDefault() is your friend here. You are basically experiencing the same problem as here. The form is submitted before the actual ajax request can be done. You need to halt the form submission, then do the ajax, and then do the form submission. If you don’t put some stops in there, it’ll just run through it and the only thing it’ll have time to do is to submit the form.

     $(document).ready(function () {
            $("form").submit(function (event) {
    
    // prevent default form submit
        event.preventDefault();
                var postcode = $.trim($("#Postcode").val());
                if (postcode.length === 0) {
                    return false;
                }
    
    
                var baseUrl = "http://dev.virtualearth.net/REST/v1/Locations/UK/";
                var apiKey = "myKey";
                var url = baseUrl + postcode + "?key=" + apiKey + "&jsonp=?";
                $.getJSON(url, function (result) {
                    if (result.resourceSets[0].estimatedTotal > 0) {
                        var location = result.resourceSets[0].resources[0].point.coordinates;
                        $("#latitude").val(location[0]);
                        $("#longitude").val(location[1]);
                        $("form").submit();
                    }
                });
            });
        });
    

    Howevern, when you put the preventDefault in there you can’t continue the form submission with $('form').submit(); anymore. You need to send it as an ajax request, or define a conditional to the preventDefault.

    Something like this perhaps:

    $(document).ready(function () {
    
        var submitForReal = false;
            $("form").submit(function (event) {
    
                var postcode = $.trim($("#Postcode").val());
                if (postcode.length === 0) {
                    return false;
                }
        // prevent default form submit
        if(!submitForReal){
        event.preventDefault();
        }else{
    
    
    
                var baseUrl = "http://dev.virtualearth.net/REST/v1/Locations/UK/";
                var apiKey = "myKey";
                var url = baseUrl + postcode + "?key=" + apiKey + "&jsonp=?";
                $.getJSON(url, function (result) {
                    if (result.resourceSets[0].estimatedTotal > 0) {
                        var location = result.resourceSets[0].resources[0].point.coordinates;
                        $("#latitude").val(location[0]);
                        $("#longitude").val(location[1]);
                        submitForReal = true;
                        $("form").submit();
                    }
                });
              }
            });
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a designer insisting on a single form field submitted by hitting enter
I have an image upload page (single page). When submitted it check filesize and
I have a nested user control which appears on every single page. It contains
I have a page which contains several forms. I want to add a single
I have a page where onclick on a button displays the text content from
I have a single web page with master detail form/input layout. Currently the form
I have a page with a single dropdown. Depending on what the user chooses
Basically I have a single page on my site that I want any php
If I have a single page with many forms of existing records: index.html.haml -
Basically I have a single page site that scrolls when the user clicks on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.