Calling a href page by passing the input fields of the form

Asked

Viewed 871 times

2

I have a page that has several inputs, when I click on the button I open a modal with the result and inside dese modal I have another button to be clicked but I need to take the form fields and pass to the other page.

My detail button code is like this:

<div><center><a href="detalhado.html"  class='btn modal-action modal-close red'><strong><i class='mdi-content-add'></i> Detalhado</a></center></strong>

Here I needed to pass the values of the sample inputs: href="detailed.html? value=100000&months=24&rate=14.15"

  • you already tried using jquery? , example: var name = $('#form01 #name'). val(); $('. modal #fieldName'). val( name );

  • But how would I do it passing Href?

1 answer

3


You can use the method .serialize() jQuery to generate the query string valor=100000&meses=24&taxa=14.15 at the click of the button:

$(function() { // Aguarda o DOM carregar
    $('.btn').on('click', function(e) {
        e.preventDefault(); // Interrompe o browser de seguir para o href do botão

        var href = $(this).attr('href'); // Armazena o valor do atributo href do botão que foi clicado

        // Concatena os valores do form ao href e redireciona para a nova url
        window.location.href = href + '?' + $('form').serialize();
    });
});

https://jsbin.com/jaxabomedi/edit?html,js,output

  • I created a blank page to test but it doesn’t work, can you tell me what pq? I tested the link you sent and it works perfect.

  • @William updated my reply. I added the code inside the function $(function() { so that jQuery will only execute the code when the DOM is loaded

  • 1

    @@Marcos Lopes worked, thank you!

Browser other questions tagged

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