【问题标题】:How To use js variable into another file如何在另一个文件中使用 js 变量
【发布时间】:2016-02-05 13:39:34
【问题描述】:

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

例如,我在 index.php 中有如下变量:

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

在另一个文件calculate.php中,我有这样的代码,想使用“vall”变量与“$total variable”相乘

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 将值发送到后端。
  • 你不能访问,你可以使用表单提交或AJAX。
  • 那么我如何在 ajax 中存储该变量
  • $.post('calculate.php', { val1 : val1 }, function(resp){ /* do something with the result */ })

标签: javascript jquery ajax multiplication


【解决方案1】:

这应该使用 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.
       }
   });
});

【讨论】:

  • 您有时间通过​​团队查看器检查我的问题吗? @Jai
猜你喜欢
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多