简体   繁体   English

alert()完成后提交表单

[英]Submit form after alert() is done

I have some jQuery code that uses Google Maps Geocoding API to convert an address to coordinates, then use alert() to show the result in popup window. 我有一些jQuery代码,这些代码使用Google Maps Geocoding API将地址转换为坐标,然后使用alert()在弹出窗口中显示结果。 Here is the code which works fine: 这是正常工作的代码:

$("#searchbox_form #search_button").click(function(e){
    e.preventDefault();
    var address = $("#location").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("input#user_lat").val(results[0].geometry.location.lat());
            $("input#user_lng").val(results[0].geometry.location.lng());
            alert("lat: " + $("input[name='user_lat']").val());
            alert("lng: " + $("input[name='user_lng']").val());
        }
    }); 



});

However now I want jQuery to submit the form $searchbox_form after the user closes the alert box. 但是现在我希望jQuery在用户关闭警报$searchbox_form后提交$searchbox_form表单。 However adding $("#searchbox_form").submit(); 但是添加$("#searchbox_form").submit(); at the end of the code chunk submits the form before the alert box shows up. 在代码块的末尾,将在警报框出现之前提交表单。 This also causes the form to be submitted before the Google Maps geocoder returns the result. 这也将导致在Google Maps地理编码器返回结果之前提交表单。

How can I allow the geocoder to return the result before the form gets submitted? 如何允许地理编码器在提交表单之前返回结果?

Same as the code above, but with 1 additional line to submit form at the end: 与上面的代码相同,但在末尾还有1行提交表单:

$("#searchbox_form #search_button").click(function(e){
    e.preventDefault();
    var address = $("#location").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("input#user_lat").val(results[0].geometry.location.lat());
            $("input#user_lng").val(results[0].geometry.location.lng());
            alert("lat: " + $("input[name='user_lat']").val());
            alert("lng: " + $("input[name='user_lng']").val());
        }
    }); 

    $("#searchbox_form").submit();  //THIS IS THE ADDED LINE OF CODE!!

});

You need to move the submit within the callback to the geocode function. 您需要将回调内的提交移至地址解析功能。 The reasoning is, it's asynchronous and not running in direct order, so it's calling the geocode and then immediately firing the submit. 原因是,它是异步的且未按直接顺序运行,因此它先调用地理编码,然后立即触发提交。 If you put it like below, the form will submit on callback and after the alerts (as alerts will block the thread). 如果按如下所示放置,则表单将在回调和警报之后提交(因为警报将阻止线程)。

$("#searchbox_form #search_button").click(function(e){
    e.preventDefault();
    var address = $("#location").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function(results, status) {
        // This function is async
        if (status == google.maps.GeocoderStatus.OK) {
            $("input#user_lat").val(results[0].geometry.location.lat());
            $("input#user_lng").val(results[0].geometry.location.lng());
            alert("lat: " + $("input[name='user_lat']").val());
            alert("lng: " + $("input[name='user_lng']").val());

            $("#searchbox_form").submit();
        }
    }); 

});

Why don't you just submit it after you're done with your callback? 完成回调之后,为什么不提交它呢?

$("#searchbox_form #search_button").click(function(e){
    e.preventDefault();
    var address = $("#location").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("input#user_lat").val(results[0].geometry.location.lat());
            $("input#user_lng").val(results[0].geometry.location.lng());
            alert("lat: " + $("input[name='user_lat']").val());
            alert("lng: " + $("input[name='user_lng']").val());
            $("#searchbox_form").submit();
        }
    }); 


});

I think geocoder.geocode is an asynchronous function. 我认为geocoder.geocode是一个异步函数。 Therefore you need the submit after the alert boxes. 因此,您需要在警报框后提交。

$("#searchbox_form #search_button").click(function (e) {
    e.preventDefault();
    var address = $("#location").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("input#user_lat").val(results[0].geometry.location.lat());
            $("input#user_lng").val(results[0].geometry.location.lng());
            alert("lat: " + $("input[name='user_lat']").val());
            alert("lng: " + $("input[name='user_lng']").val());

            $("#searchbox_form").submit();
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM