简体   繁体   English

如何在函数外部获取变量的值?

[英]How can I get the value of the variable outside of the function?

I have this function and I need get the value of the variable $overall outside of the function, how can I do it? 我有此功能,我需要在该功能之外获取变量$ overall的值,该怎么办?

$(function(){
     function ca($tr_value, $qty ){
     window.$overall = 0;

    $($tr_value).each(function() {

        var $qnt = $(this).find($qty);
        var $price = $(this).find("td").eq(2);

        var sum = parseFloat($price.text()) * parseFloat($qnt.val());

        $(this).find("td").eq(3).text(sum);
        $(this).find(".priceitem").attr('value', $(this).find("td").eq(2).text());
        $(this).find(".totalitem").attr('value', sum);

        $overall += sum;

        return window.overall;

    });

    $("#total").text($overall);
    $(".total").attr('value', $overall);
}

    function ca_transp(){
    var $overall_transp = 0;

    $("tr.sum_transp").each(function() {

        var $qnt = $(this).find(".qty_transp");
        var $price = $(this).find("td").eq(2);

        var sum = parseFloat($price.text()) * parseFloat($qnt.val());

        $(this).find("td").eq(3).text(sum);
        $(this).find(".priceitem").attr('value', $(this).find("td").eq(2).text());
        $(this).find(".totalitem").attr('value', sum);

        $overall_transp += sum;

    });

    $("#total_transp").text($overall_transp);
    $(".total").attr('value', $overall_transp);

}

//]]>  

   function suma_total(){
    var $suma_total = 0;    

    $("tr.total").each(function() {

        //var $qnt = $(this).find(".total");

        var $total_parcial = $(this).find("td").eq(1);
        var sum = parseFloat($total_parcial.text());


        $(this).find("td").eq(1).text(sum);
        $suma_total += sum;


    });





    $("#total_cotizacion").text($suma_total);
    $(".total_cotizacion").attr('value', $suma_total);

}
    ca_transp();
    $('input.qty_transp').bind('change keyup',function(){ca_transp();});
    ca("tr.sum", ".qty");
    $('input.qty').bind('change keyup',function(){ca("tr.sum", ".qty");});
    alert($overall);

});

Thanks for your help. 谢谢你的帮助。

Define it outside the function, this will make it global though. 在函数外部定义它,尽管如此,它将使它成为全局的。

$(function(){
    var $overall;
    function ca($tr_value, $qty ){
       $overall = 0;
       $($tr_value).each(function() {

PS you are also nesting document.ready functions. PS您还嵌套document.ready函数。 You don't need to do that. 您不需要这样做。 Infact your code can be cleaned up quite a bit. 实际上,您的代码可以清理很多。 I don't think you need to create a global $overall variable. 我认为您无需创建全局$overall变量。 Your code is not structured right at the moment, if you make it clear what your intent is then i can revise it. 您的代码目前还没有结构化,如果您清楚您的意图,那么我可以对其进行修改。

It's simple scope question. 这是一个简单的范围问题。

Options: 选项:

  1. Move the variable to the global scope 将变量移到全局范围
  2. Store the value in a hidden element in the DOM. 将值存储在DOM中的隐藏元素中。
  3. Make them share the same scope. 使它们共享相同的范围。

Rather than creating an evil global variable , you could return it from your ca function. 与其创建一个邪恶的全局变量 ,不如从ca函数中返回它。

$(function () {
   //$overall doesn't need to be the same name here, you could call it "$returnedOverall" for example, and it would still work
   var $overall = ca("tr.sum", ".qty");
   alert($overall); //will show you the value of $overall, returned from the above call
   $('input.qty').bind('change keyup', function () {
       ca("tr.sum", ".qty");
       var $overall2 = ca("tr.sum", ".qty");
       alert($overall2); //will show you the value of $overall2, returned from the above call
   });
});

//ca doesn't need to be in your jQuery document.ready function, it only needs to be called from within document.ready
function ca($tr_value, $qty) {
    var $overall = 0;
    $($tr_value).each(function () {
        var $qnt = $(this).find($qty);
        var $price = $(this).find("td").eq(2);
        ...
        $overall += sum;
        //don't return it here, this is inside your jQuery.each function
    });
    //return it here, from your ca function
    return $overall;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将函数内部变量的值获取到 on jquery 外部 - how can i get the value of variable inside function to outside the on jquery 如何在 JS 中运行 GET 请求并将值存储在 GET 请求之外的 JS 变量中 function - How can I run a GET Request in JS and store the value in a JS variable outside of the GET Request function "如何获取mern堆栈中箭头函数之外的变量值" - how can i get the value of a variable outside the arrow function in mern stack 如何获取Javascript中EventListener function之外的变量值? - How do I get the variable value outside of EventListener function in Javascript? 如何开始在$ .get()函数外部使用变量? - How can I start using variable outside $.get() function? 如何在函数IN jquery之外获取变量值 - How to get variable value outside a function IN jquery 如何在骨干渲染功能之外获取clientHeight值? - How can I get clientHeight value outside backbone render function? 我如何获得“ j”的值以在函数外部使用? - How I can get the value of “j” for use outside the function? 我如何在函数外获取数组的值-Angular.js - How can I get the value of array outside the function - Angularjs 节点 JS - 如何获取 function 之外的值? - Node JS - How can I get the value outside the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM