找到你要的答案

Q:Can I use a callback with .submit in jquery

Q:我可以用一个回调,提交在jQuery

I'm trying to prevent a return false or undefined from the submit handler. I only want to return a value after the async results come back from google maps. Is this possible? My objective is to wait for google map results to come back before I go to my controller.

$('form').submit(function (e) {
        
        var geocoder = new google.maps.Geocoder();
        var address = $('#SearchQuery').val();
        
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status != google.maps.GeocoderStatus.OK) {
                
                alert("Geocode was not successful for the following reason: " + status);
                return false;
            } else {
                $("#latitude").val(results[0].geometry.location.G);
                $("#longitude").val(results[0].geometry.location.K);
                
                alert("Geocode successful");
                return true;
            }
        });
        //don't let anything get returned here, is a callback needed?
    });

我试图阻止返回false或未定义的提交处理程序。我只想在异步结果回来从谷歌地图返回一个值。这可能吗?我的目标是等待谷歌地图的结果回来之前,我去我的控制器。

$('form').submit(function (e) {
        
        var geocoder = new google.maps.Geocoder();
        var address = $('#SearchQuery').val();
        
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status != google.maps.GeocoderStatus.OK) {
                
                alert("Geocode was not successful for the following reason: " + status);
                return false;
            } else {
                $("#latitude").val(results[0].geometry.location.G);
                $("#longitude").val(results[0].geometry.location.K);
                
                alert("Geocode successful");
                return true;
            }
        });
        //don't let anything get returned here, is a callback needed?
    });
answer1: 回答1:

The use of your return inside a closed callback does not return to the outer submit callback. So basically there is nothing preventing the form submitting without the geocoding being done

What you could do is prevent the jQuery submit event and within the geocode success submit the form through native javascript submit which does not trigger the jQuery version

 var geocoder = new google.maps.Geocoder();

 $('form').submit(function (e) {
     // cancel the event to prevent immediate submit
     e.preventDefault();

     var form = this;
     var address = $('#SearchQuery').val();

     geocoder.geocode({
         'address': address
     }, function (results, status) {
         if (status != google.maps.GeocoderStatus.OK) {
             alert("Geocode was not successful for the following reason: " + status);

         } else {
             $("#latitude").val(results[0].geometry.location.G);
             $("#longitude").val(results[0].geometry.location.K);

             alert("Geocode successful");

             // do submit through native submit
             form.submit();

         }
     });

 });

在一个封闭回调中使用返回不返回外部提交回调。所以基本上没有什么阻止表单提交不匹配了

你所能做的就是防止jQuery提交事件,在匹配成功提交表单通过本地JavaScript提交不触发jQuery版本

 var geocoder = new google.maps.Geocoder();

 $('form').submit(function (e) {
     // cancel the event to prevent immediate submit
     e.preventDefault();

     var form = this;
     var address = $('#SearchQuery').val();

     geocoder.geocode({
         'address': address
     }, function (results, status) {
         if (status != google.maps.GeocoderStatus.OK) {
             alert("Geocode was not successful for the following reason: " + status);

         } else {
             $("#latitude").val(results[0].geometry.location.G);
             $("#longitude").val(results[0].geometry.location.K);

             alert("Geocode successful");

             // do submit through native submit
             form.submit();

         }
     });

 });
javascript  jquery  asp.net-mvc  callback