Problem with ajax form

0

I am learning to use ajax when doing click on the submit button of the form does nothing.

I put my code:

// Esta es mi función:
$("#virgen").click(function virgen() {
consulta = $("#virgen").val();
$.ajax({
  data: 'b=' + consulta,
  type: "POST",
  url: "servidor.php",
  success: function(data) { // es el parametro que te devuelve
    var variable = JSON.parse(data);
    lat1 = variable.lat1;
    lng1 = variable.lng1;
  }
})
map.removeMarker(markers[0]);
markers[0] = map.addMarker({
  lat: lat1,
  lng: lng1
}); // pone marcador en mapa

enlazarMarcador(s);
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="button" id="virgen" value="CIFP Virgen de gracia" class="boton" onclick="virgen()">

I've checked that all the imported bookstores are

Thanks in advance.

    
asked by Nuria Nieto 21.11.2018 в 10:50
source

1 answer

1

When jquery is done $ ('# idButton'). click attaches the click event to the button automatically not being necessary both the declaration of the function name and the onclick event on the button.

I have also removed a parentheses closure ")" that was left over.

$("#virgen").click(function () {
    consulta = $("#virgen").val();
    $.ajax({
        data: 'b=' + consulta,
        type: "POST",
        url: "servidor.php",
        success: function (data) { // es el parametro que te devuelve
            var variable = JSON.parse(data);
            lat1 = variable.lat1;
            lng1 = variable.lng1;
        }
    })
    map.removeMarker(markers[0]);
    markers[0] = map.addMarker({ lat: lat1, lng: lng1 });  // pone marcador en mapa

    enlazarMarcador(s);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="virgen" value="CIFP Virgen de gracia" class="boton">

I take this opportunity to tell you that when creating the function within the jquery click event, what you are creating is a function in another context that can not be viewed from outside the context (from outside the event).

    
answered by 21.11.2018 в 10:56