简体   繁体   中英

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?

$(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. 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. 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.
  3. Make them share the same scope.

Rather than creating an evil global variable , you could return it from your ca function.

$(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;
}

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