How to concatenate serialized data from a form to the javascript string?

Asked

Viewed 98 times

-1

How can I concatenate a serialized data variable var dados = $(form).serialize(); with a second variable, for example var x ="2222"?

Thank you for your attention! Horatio

1 answer

1

Just take the result of the serialized variable and concatenate with the existing one:

$(document).ready(function(){
    $("button").click(function(){
        var dados = $("form").serialize();
        var x = "2222";
        
        $("h1").text(dados+"&x="+x);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form action="">
  Nome: <input type="text" name="name" value="Fulano"><br>
  Idade: <input type="text" name="idade" value="34"><br>
</form>
<br>
<button>Serializar</button>

<h1></h1>

Browser other questions tagged

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