简体   繁体   English

在Symfony中使用JQuery设置Input OnSubmit的值

[英]Setting Value of Input OnSubmit using JQuery in Symfony

I am trying to set a onsubmit function that will use the google.maps.Geocoder to set the gps positions using the address entered by a user, my code is the following 我正在尝试设置一个onsubmit函数,该函数将使用google.maps.Geocoder通过用户输入的地址来设置gps位置,我的代码如下

    <form onsubmit="getGPSLocation()">
            <input type='text' id='search_address' />
            <input type='text' id='search_lat' hidden='hidden'/>
            <input type='text' id='search_lon' hidden='hidden'/>
            <input type='submit'/>
    </form> 

    <script>
    function getGPSLocation(){
        var string = $('#search_address').val();
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': string
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var pos = results[0].geometry.location;
                $('#search_lat').attr('value',pos.lat());
                $('#search_lon').attr('value',pos.lng());
            }
        });

    }
    </script>

The problem is that when i click submit, it does properly call the geocoder function, but the problem is that: 问题是,当我单击提交时,它确实会正确调用geocoder函数, 问题是:

  • Before the geocoder can make the request to the google server and return the coordinate data, the Submit function has already redirected the page. 在地址解析器可以向Google服务器发出请求并返回坐标数据之前,Submit函数已经重定向了页面。

So the question is: 所以问题是:

How to delay the submitting of the information untill the geocoder function has successfully completed its action . 如何延迟信息的提交,直到地址解析器功能成功完成其操作为止

You may use the Jquery onsubmit function. 您可以使用Jquery onsubmit函数。 Try this code : 试试这个代码:

$('#your_form_id').submit(function() {

 var string = $('#search_address').val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': string
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var pos = results[0].geometry.location;
            $('#search_lat').attr('value',pos.lat());
            $('#search_lon').attr('value',pos.lng());
        }
    });

  return;
 });

Hope this helps ! 希望这可以帮助 !

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

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