Submit a form by clicking on a link

0

I have the following problem, I have the following piece of code.

 <li>
       <form action="ControladorUsuario" method="POST" id="cerrarSesion">
           <input type="hidden" name="uAccion" value="Salir">
           <a href="#" id="salir"><i class="fa fa-fw fa-power-off"></i> Salir</a>
       </form>

</li>

I need to do the form submit, when I click the link. I have done with jquery

$('#salir').click(function(){
    $('#cerrarSesion').submit();

});

Also putting the submit in an onclick in the link, but I do not get anything ... Any suggestions? Greetings

EDIT: Now it works for me, remove the form completely and put it back and I added this:

    $('#salir').click(function(){

           $('#cerrar').submit(); 

    });

Thanks for the answers;)

    
asked by urrutias 10.06.2017 в 15:09
source

1 answer

1

A simple way with pure JavaScript would be:

<script>
    function cerrar() {
    document.cerrarSesion.submit();
    }
</script>

<li>
  <form action="ControladorUsuario" method="POST" id="cerrarSesion">
    <input type="hidden" name="uAccion" value="Salir">
    <a href="#" id="salir" onclick="cerrar();"><i class="fa fa-fw fa-power-off"></i> Salir</a>
  </form>
</li>

A more simple and portable version:

 <li>
  <form action="ControladorUsuario" method="POST" id="cerrarSesion">
    <input type="hidden" name="uAccion" value="Salir">
    <a href="#" id="salir" onclick="this.form.submit()"><i class="fa fa-fw fa-power-off"></i> Salir</a>
  </form>
</li>

How it works:

We use the property submit() and the this to refer to the current form, followed by form .

If it still does not work:

<li>
  <form action="ControladorUsuario" method="POST" id="cerrarSesion">
    <input type="hidden" name="uAccion" value="Salir">
    <a href="#" id="salir" onclick="document.getElementById('cerrarSesion').submit();"><i class="fa fa-fw fa-power-off"></i> Salir</a>
  </form>
</li>
    
answered by 10.06.2017 в 15:19