简体   繁体   English

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

[英]jQuery: performing some action before form is submitted

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

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:我目前的方法是将提交事件的处理程序添加到唯一的表单,然后在我完成后调用 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();
            }
        });
    });
});

event.preventDefault() is your friend here. event.preventDefault()是你的朋友。 You are basically experiencing the same problem as here .您基本上遇到了与此处相同的问题。 The form is submitted before the actual ajax request can be done.在实际的 ajax 请求可以完成之前提交表单。 You need to halt the form submission, then do the ajax, and then do the form submission.您需要停止表单提交,然后执行 ajax,然后执行表单提交。 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();但是,当您将preventDefault放在那里时,您无法使用$('form').submit(); anymore.了。 You need to send it as an ajax request, or define a conditional to the preventDefault .您需要将其作为 ajax 请求发送,或为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();
                }
            });
          }
        });
    });

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

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