Calling Javascript function after Page loading

Asked

Viewed 431 times

0

Good morning to all

I have a function to multiply qnt * total, but it only loads after clicking the field vrunitario. I wish that when opening the page she would calculate, I don’t know where I went wrong here in the code.

inserir a descrição da imagem aqui

 $("#tbodyCotacao").on("blur keyup load",'input[id^="pqnt"],input[id^="vrunit01"],input[id^="vrunit02"],input[id^="vrunit03"]', function (event) {
  multiplicar($(this).closest("tr"));});

 function multiplicar(row) {
  var vlund01 = row.find('input[id^="vrunit01"]').val();
  //retira separadores de milhar ponto
  vlund01 = vlund01.split(".").join("");
  //substitui separador decimal virgula por ponto
  vlund01=vlund01.replace(",", ".");
  vlund01 = +vlund01;
  var qnd = +row.find('input[id^="pqnt"]').val();
  //total para uso nos calculos
  //2 casas decimais
  var total01 = (vlund01 * qnd).toFixed(2);
  row.find('input[id^="subtotal01"]').val(total01);
  //totalS para uso na apresentação substitui separador decimal ponto por virgula
  totalS01=total01.replace(".", ",");
  //a regex abaixo coloca um ponto a esquerda de cada grupo de 3 digitos desde que não seja no inicio do numero
  row.find('input[id^="subtotal01T"]').val((totalS01).replace(/\B(?=(\d{3})+(?!\d))/g, "."));
}

PROVISIONAL MEASURE People I made a provision measure, to appear and improved but I would like that appeared in the page loading.

$(document).ready(function () {
  $("#tbodyCotacao").on("change keyup keydown paste propertychange bind mouseover",'input[id^="pqnt"],input[id^="vrunit01"],input[id^="vrunit02"],input[id^="vrunit03"]', function (event) {
  multiplicar($(this).closest("tr"));     
});

function multiplicar(row) {
  var vlund01 = row.find('input[id^="vrunit01"]').val();
  //retira separadores de milhar ponto
  vlund01 = vlund01.split(".").join("");
  //substitui separador decimal virgula por ponto
  vlund01=vlund01.replace(",", ".");
  vlund01 = +vlund01;
  var qnd = +row.find('input[id^="pqnt"]').val();
  //total para uso nos calculos
  //2 casas decimais
  var total01 = (vlund01 * qnd).toFixed(2);
  row.find('input[id^="subtotal01"]').val(total01);
  //totalS para uso na apresentação substitui separador decimal ponto por virgula
  totalS01=total01.replace(".", ",");
  //a regex abaixo coloca um ponto a esquerda de cada grupo de 3 digitos desde que não seja no inicio do numero
  row.find('input[id^="subtotal01T"]').val((totalS01).replace(/\B(?=(\d{3})+(?!\d))/g, "."));}
  • 1

    In jQuery you can put your code inside the function ready in the document which will be executed when the document is loaded. Example: $(document).ready(function(){ //seu código }); or just $(function(){ //seu código });

  • I don’t understand this part: $(this).closest("tr"). It’s all in one tr? By the image I’m seeing several lines and not just one. Or is it all within one td in one line?

  • That’s because the event load will not work with div, so it would be necessary to create it separately and call the function multiplicar(row).

  • Why are you wearing ^= on the selectors? Are several of these forms on the screen at the same time? That one vrunitario is unique? What id of it? To make the event load work you need to know what to send in the variable row of function.

  • Would have to do $(window).on("load", function(){ multiplicar(row); });... the problem is: what to send in this row since you don’t have the $(this) of the other events blur and keyup that you created?

  • Lercio - I tried to do it didn’t work.

  • Sam - It is a dynamic table, insert and remove lines to be saved in the bank. To enter worked perfectly, to show that this is giving this inconvenience. The load event was testing to make it load the calculation. I already made the screen to insert, this screen would be to edit, then you have to load what is saved in the bank and have option to insert the remove lines.

Show 2 more comments
No answers

Browser other questions tagged

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