jQuery:在提交表单之前执行一些操作

Saq*_*qib 3 javascript ajax maps jquery geocode

我有一个页面,上面有一个表格.表单包含一个文本框和一个提交按钮.

提交表单时,通过单击按钮或按文本框中的Enter键,我想进行查找(在这种情况下,使用Bing Maps对邮政编码进行地理编码),然后像往常一样将表单提交到服务器.

我目前的方法是将提交事件的处理程序添加到一个唯一的表单,然后在我完成时调用submit(),但我无法使其工作,并且无法调试问题:

$(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();
            }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

Nik*_*las 5

event.preventDefault()是你的朋友.您基本上遇到的问题与此处相同.在实际的ajax请求完成之前提交表单.您需要暂停表单提交,然后执行ajax,然后执行表单提交.如果你没有在那里设置一些停靠点,那么它将只是通过它,它唯一有时间做的就是提交表单.

 $(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();
                }
            });
        });
    });
Run Code Online (Sandbox Code Playgroud)

然而,当你把它preventDefault放在那里时,你再也无法继续提交表单$('form').submit();了.您需要将其作为ajax请求发送,或者为其定义条件preventDefault.

也许这样的东西:

$(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();
                }
            });
          }
        });
    });
Run Code Online (Sandbox Code Playgroud)