Save variable in Javascript and pass in getjSon URL

Asked

Viewed 494 times

0

Good people I am new to Javascript, and I need to save the value of the matricule field in a variable and pass it in the URL of a getJson. How can I do that? Follow the code below the two functions: Code to save the registration variable:

$(function($){
    $('#btn_entrar').click(function(){
        var matricula = $('#id_matricula').val();
    });
);

Code to call my getJson:

$("#btn_entrar").click(function(event){
    $(document).ready(function(){
        $.getJSON("URL/'ValorDaVariavelMatricula'", function(data){
        });
    });
 });
  • 1

    Because you have two events click to the same button #btn_entrar? Has to join together both.

2 answers

1

It seems to me that you are creating two events for the same button #brn_entrar. Just merge the codes. You take the field value and put it into the variable matricula, then call the getJSON concatenating this variable to the url string:

$(function(){
    $('#btn_entrar').on("click", function() {
        var matricula = $('#id_matricula').val();

        $.getJSON("URL/" + matricula, function(data){
        });
    });
});

-2

Hello, friend, I think the best solution would be to use ajax, follows below the code:

<script type="text/javascript">
$(document).ready(function() {
    $('#btn_entrar').click(function(){
        var matricula = $('#id_matricula').val();
        $.ajax({
            url: URL,
            type: 'GET',
            dataType: 'json',
            data: {matricula: matricula},
            success: function(json){
                // aqui trata seu json
            },
            error: function(json){
                alert('erro');
            }
        });     
    });

});
</script>

Browser other questions tagged

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