简体   繁体   中英

Pass Variable From Jquery to ajax function

I have the code below:

$('#bayar').click(function(){    

    var totalHarga=0;

    $('.totalHarga').each(function () {
       totalHarga+=parseFloat($(this).text().trim());
    });

var kodeTrx=$('#kodeTransaksi').val().trim();
var diskon=$('#diskonPembelian').val().trim();
var grandTotal=totalHarga-diskon;
var diskonPersen=grandTotal/totalHarga;
var jumlahBayar=$('#jumlahBayar').val().trim();
var pelanggan = $('#pelanggan').val().trim();
var sisaBayar=jumlahBayar-grandTotal;

$.ajax({

  type : "post",
  url : "input_nota_penjualan.php",
  data : {
        kodeTransaksi: kodeTrx, totalTransaksi:totalHarga,      
        diskonTransaksi:diskon, GrandTotalTransaksi:grandTotal, 
        bayarTransaksi:jumlahBayar, pelanggan:pelanggan,  
        sisaTransaksi:sisaBayar
      },
  success: function (response) {            
    alert('Suksessss');        
  },
  });

};

the jquery variable is not readable by ajax, or the value in the ajax function is empty. How to get variables from jquery to read in ajax function block?

You wrongly close the function,please check last line of the below code,

$('#bayar').click(function(){    

var totalHarga=0;

$('.totalHarga').each(function () {
   totalHarga+=parseFloat($(this).text().trim());
});

  var kodeTrx=$('#kodeTransaksi').val().trim();
  var diskon=$('#diskonPembelian').val().trim();
var grandTotal=totalHarga-diskon;
var diskonPersen=grandTotal/totalHarga;
var jumlahBayar=$('#jumlahBayar').val().trim();
var pelanggan = $('#pelanggan').val().trim();
var sisaBayar=jumlahBayar-grandTotal;

$.ajax({

  type : "post",
  url : "input_nota_penjualan.php",
  data : {
        kodeTransaksi: kodeTrx, totalTransaksi:totalHarga,      
        diskonTransaksi:diskon, GrandTotalTransaksi:grandTotal, 
        bayarTransaksi:jumlahBayar, pelanggan:pelanggan,  
        sisaTransaksi:sisaBayar
      },
  success: function (response) {            
    alert('Suksessss');        
  },
  });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM