Run JS when opening page [closed]

1

Good evening. I have a js where I need it to run when loading the page. It works when I click, but I can not get the calculation done automatically when loading the page.

$(document).ready(function(){
    executa5();
    $(".cubagem, .tabbable").on('click mouseover load', executa5);

        function executa5(){
            valorTotaldaNota    = $("#valorTotaldaNota").val();
            valorTotaldaNota    = valorTotaldaNota.replace(".", "");
            valorTotaldaNota    = valorTotaldaNota.replace(",", ".");
            adValorem           = $("#adValorem").val();
            gris                = $("#gris").val();

            v1   = valorTotaldaNota * adValorem / 100;
            v2   = valorTotaldaNota * gris / 100;
            v1v2 = v1+v2;

            $("#valordoSeguro").val( v1v2.toFixed(2) );
    };  
});

What am I doing wrong?

    
asked by anonymous 24.06.2015 / 00:08

2 answers

1

Tiago , there's nothing wrong with your logic ( link )

Another code is preventing completion (ie the function is saved in memory but the document is not ready).

To debug using the inspector, go to the Sources tab, choose the document containing the javascript and add a break point in the call:

$(document).ready(function(){
executa5(); // <-- adicione o break point aqui

Then refresh the page to see if the call executed5 () is passed on the load. If it is not, enter the break point at another time in the javascript and check successively where the code no longer passes. Take step by step by typing F11 and F10 .

    
24.06.2015 / 00:37
0

Are you using which version of jQuery ?

The on method was only added in version 1.7+. Make sure you are importing the correct version.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

Example:

function executa5(){
  alert("entrou");
  valorTotaldaNota    = $("#valorTotaldaNota").val();
  valorTotaldaNota    = valorTotaldaNota.replace(".", "");
  valorTotaldaNota    = valorTotaldaNota.replace(",", ".");
  adValorem           = $("#adValorem").val();
  gris                = $("#gris").val();

  v1   = valorTotaldaNota * adValorem / 100;
  v2   = valorTotaldaNota * gris / 100;
  v1v2 = v1+v2;

  $("#valordoSeguro").val( v1v2.toFixed(2) );
}  

$(document).ready(function(){
    executa5();
    $(".cubagem, .tabbable").on('click mouseover load', executa5);
});
.cubagem {
  height: 50px;
  width: 50px;
  background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script><divclass="cubagem">
</div>

<input id="valorTotaldaNota" value="100" /> <br/>
<input id="adValorem" value="5" /> <br/>
<input id="gris" value="20" /> <br/>
<input id="valordoSeguro" />
    
24.06.2015 / 00:50