![](/img/trans.png)
[英]how can i get the value of variable inside function to outside the on jquery
[英]How can I get the value of the variable outside of the function?
我有此功能,我需要在該功能之外獲取變量$ 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);
});
謝謝你的幫助。
在函數外部定義它,盡管如此,它將使它成為全局的。
$(function(){
var $overall;
function ca($tr_value, $qty ){
$overall = 0;
$($tr_value).each(function() {
PS您還嵌套document.ready
函數。 您不需要這樣做。 實際上,您的代碼可以清理很多。 我認為您無需創建全局$overall
變量。 您的代碼目前還沒有結構化,如果您清楚您的意圖,那么我可以對其進行修改。
這是一個簡單的范圍問題。
選項:
與其創建一個邪惡的全局變量 ,不如從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.