Force the keypress enter or Submit form into form with JS, Geolocation!

Asked

Viewed 209 times

0

Guys, I have the following problem to solve.. I’m using the google API to pick up the user’s current geolocation, but when you click on the icon that picks the geolocation the form is only submitted if you click the input and press enter, but I need this to be automatic. 1 - Or press enter automatically. 2 - Or Ubmit in the form automatically. already tested the following codes but does not return the expected result:

//for submit
jQuery("form#searchUnits").submit();

//for keypress Enter
var e = jQuery.Event( "keypress", { which: 13 } );
jQuery('form#searchUnits .units-input').trigger( e );

this code is inside a jQuery(Document). click(Function(e){}); with e.preventDefault(); then everything happens without recharging the page. Thank you!

                    // Geolocation Google Maps API
                    jQuery('#getLocation').on('click', function ( e ) {
                        e.preventDefault();

                        if ( navigator.geolocation ) {
                            navigator.geolocation.getCurrentPosition( function ( position ) {
                                var pos = {
                                    lat: position.coords.latitude,
                                    lng: position.coords.longitude
                                };

                                var geocoder = new google.maps.Geocoder;
                                geocoder.geocode({ 'location': pos }, function ( results, status ) {
                                    if ( status === 'OK' ) {
                                        if ( results[1] ) {

                                            jQuery( '#getLocation' ).parent().find( 'input' ).val( results[1].formatted_address );
                                            jQuery( '#getLocation' ).parent().find( 'input' ).attr( 'lat', pos.lat );
                                            jQuery( '#getLocation' ).parent().find( 'input' ).attr( 'lng', pos.lng );


                                            //arrayLat();
                                        } else {
                                            //window.alert('No results found');
                                        }
                                    } else {
                                    }
                                });
                            }, function () {
                                alert('É necessário ativar a geolocalização para utilizar este recurso!');
                            });
                        } else { }


                        jQuery( "html, body" ).click();
                            jQuery( "form#searchUnits .units-input" ).focus();

                            jQuery("form#searchUnits").submit();

                            var e = jQuery.Event( "keypress", { which: 13 } );
                            jQuery('form#searchUnits .units-input').trigger( e );
                    });

  • Voce has callback after obtaining Geolocation?

  • What do you mean the callback ? it returns me the address, lat and lng.

  • Put the relevant part of how you’re getting the location

  • ta ae brother! vlw

  • try submitting the form within the status ok. if (status === 'OK') in the latter line of that if place jQuery("form#searchUnits").submit();

  • I tried that, it didn’t work.. = / it fills the input with the location.. but only gives Submit if I click enter manually..

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.