Avoid accumulating in a string by repeatedly clicking a button

0

I have the following code that works as follows:

Clicking the button returns a string aaaa,bbbbb which are the values of the inputs.

Clicking for the second time returns aaaa,bbbbb,aaaa,bbbbb and so on.

The question: how to always return aaaa,bbbbb regardless of how many clicks are given

var strTotal2 ="";
$(document).ready(function(){
    $("#bsubmit").click(function(){
        $("input[class^='outro-form-control']").each(function(){
            var str2 = ($(this).val());
            strTotal2 = strTotal2+","+str2;
        });
        myString2 = strTotal2.substring(1);
        console.log(myString2);
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="sheets" name="sheets" class="contact-form row" method="post" action="c_form/envia.php" onsubmit="return false">
<input id="nome" name="nome" value="aaaa" class="outro-form-control" placeholder="Insira seu nome">
<input id="nation" name="email" value="bbbbb"  class="outro-form-control">
<button type="submit" id="bsubmit" value="Submit" name="submit" class="btn btn-success pull-right">Enviar</button>
<form>
    
asked by anonymous 19.09.2017 / 03:44

1 answer

1

The whole question deserves a response and will be that of Isac's comment.

var strTotal2=""; must be inside the submit.

$(document).ready(function(){
    $("#bsubmit").click(function(){
        var strTotal2 ="";
        $("input[class^='outro-form-control']").each(function(){
            var str2 = ($(this).val());
            strTotal2 = strTotal2+","+str2;
        });
        myString2 = strTotal2.substring(1);
        console.log(myString2);
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="sheets" name="sheets" class="contact-form row" method="post" action="c_form/envia.php" onsubmit="return false">
<input id="nome" name="nome" value="aaaa" class="outro-form-control" placeholder="Insira seu nome">
<input id="nation" name="email" value="bbbbb"  class="outro-form-control">
<button type="submit" id="bsubmit" value="Submit" name="submit" class="btn btn-success pull-right">Enviar</button>
<form>
    
19.09.2017 / 23:10