[英]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: 选项:
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.