Skip to content Skip to sidebar Skip to footer

Jquery - Form Not Being Submitted

I have some code which, when the search box is clicked, geocodes a location (unless laready done by the autosuggest) in the location box and should the submit the form. The problem

Solution 1:

I don't know what processLocation(query) expects for query, but idea would be this:
1. change function signature

function processLocation(query, doSubmit) { // <--- new parameter
    var query = $.trim(input.val()),
        geocoder;

    if (!query || query == lastQuery) {
        console.log("Empty or same variable");
        return;
    }

    lastQuery = query;

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        address: query
    }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            lat.val(results[0].geometry.location.lat());
            lng.val(results[0].geometry.location.lng());
            if (doSubmit){
                 $('#searchform').submit(); //<-- new param usage
            }
        } else {
            alert("We couldn't find this location. Please try an alternative");
        }
    });
}

2. remove this call

$('#searchform').on('submit', function (event) {
    alert('Submitted')
    event.preventDefault(); 
});

3. add this call

$('#search').click(function(){
     processLocation(new Date(), true); //<-- don't know what's the first param
});

Tried to play around in your jsfiddle but had no success as I was constantly getting Empty or same variable message into console. I think you know your logic better and you'll figure out


Solution 2:

I would just have the submit button disabled until the values are valid?

<input type="submit" value="Search" id="search" disabled="disabled">

search.removeAttr('disabled')

http://jsfiddle.net/sR4GR/38/

?


Post a Comment for "Jquery - Form Not Being Submitted"