如何将 js 变量用于另一个文件

How To use js variable into another file

本文关键字:用于 另一个 文件 变量 js      更新时间:2023-09-26

我有一个Javascript变量,它在更改时有一些价值。现在我想在另一个文件中使用此变量进行一些计算。

例如,我在索引中有我的变量.php如下所示:

    $(function() {
       $("#sel").on("change", function() {
       var vall = $(this).find('option:selected').attr('value')
       alert(vall);
    });
   });

在另一个计算文件中.php我有这样的代码,并希望使用"vall"变量与"$total变量"进行倍数

public function getGrandTotal(){
    $quote = Mage::getModel('checkout/session')->getQuote();
    $total = $quote->getGrandTotal();
    $gt =  $total *  var vall ; // "vall" variable to multiply with total
    return Mage::helper('checkout')->formatPrice($gt);
}

这应该在 Ajax 上完成,如下所示:

$("#sel").on("change", function() {
   var vall = $(this).val();
   $.ajax({ 
       type:'post',
       url : "url to call",
       data:{val : vall}, // send vall here get as val in the backend.
       dataType:'text', // other values are json, script, html etc.
       success:function(data){
          console.log(data);  // <----get the response from the backend if echoed.
       }
   });
});
相关文章: