Javascript jQuery-变量范围问题
我有以下javascript:Javascript jQuery-变量范围问题,javascript,jquery,variables,scope,Javascript,Jquery,Variables,Scope,我有以下javascript: $.getJSON('/calculate_quote/' + moulding_1_id, function(data) { moulding_1_cost = data.moulding.cost; moulding_1_width = data.moulding.width; }); cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
问题在于,在getJSON调用之外,两个变量moulding_1_cost
和moulding_1_width
是未定义的。如何使这两个变量在getJSON调用之外可用?在回调运行之前(当服务器返回JSON数据时),不会设置变量,因此需要从回调调用使用它们的任何代码,如下所示:
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
var moulding_1_cost = data.moulding.cost;
var moulding_1_width = data.moulding.width;
var cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
});
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
someFunction(data.moulding.cost, data.moulding.width);
});
function someFunction(mqc, m1w) {
var cost_of_moulding = ( ( 2 * ( width + ( 2 * m1w) ) + 2 * ( height + ( 2 * m1w) ) ) / 1000 ) * m1c;
$('#item_total').html( cost_of_moulding );
}
或者调用另一个函数,如下所示:
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
var moulding_1_cost = data.moulding.cost;
var moulding_1_width = data.moulding.width;
var cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
});
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
someFunction(data.moulding.cost, data.moulding.width);
});
function someFunction(mqc, m1w) {
var cost_of_moulding = ( ( 2 * ( width + ( 2 * m1w) ) + 2 * ( height + ( 2 * m1w) ) ) / 1000 ) * m1c;
$('#item_total').html( cost_of_moulding );
}
在这两种情况下,仍然正确的是,您需要在获得数据后触发任何使用数据的操作,所有异步操作都是这样的。在回调运行之前(当服务器返回JSON数据时),不会设置变量,因此您需要从该回调调用使用它们的任何代码,如下所示:
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
var moulding_1_cost = data.moulding.cost;
var moulding_1_width = data.moulding.width;
var cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
});
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
someFunction(data.moulding.cost, data.moulding.width);
});
function someFunction(mqc, m1w) {
var cost_of_moulding = ( ( 2 * ( width + ( 2 * m1w) ) + 2 * ( height + ( 2 * m1w) ) ) / 1000 ) * m1c;
$('#item_total').html( cost_of_moulding );
}
或者调用另一个函数,如下所示:
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
var moulding_1_cost = data.moulding.cost;
var moulding_1_width = data.moulding.width;
var cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
});
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
someFunction(data.moulding.cost, data.moulding.width);
});
function someFunction(mqc, m1w) {
var cost_of_moulding = ( ( 2 * ( width + ( 2 * m1w) ) + 2 * ( height + ( 2 * m1w) ) ) / 1000 ) * m1c;
$('#item_total').html( cost_of_moulding );
}
在这两种情况下,仍然正确的是,一旦获得数据,您就需要触发使用数据的任何操作,所有异步操作都是这样的。add
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
var moulding_1_cost;
var moulding_1_width;
在任何javaScript函数之外;) 添加
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
var moulding_1_cost;
var moulding_1_width;
在任何javaScript函数之外;) 您应该在getJSON调用中执行所有操作,以确保它以正确的顺序发生。您应该在getJSON调用中执行所有操作,以确保它以正确的顺序发生。事实上,它们不是未定义的(在代码执行之后)。通过跳过
var
关键字,这些名称直接进入全局范围(在大多数情况下..是窗口
)。因此,在执行此脚本后,您可以从脚本的任何位置访问窗口
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
这很可能是你的问题,时间问题。由于这是ajax请求的成功处理程序
,因此此代码异步运行,因此不会立即执行
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
要解决这个问题,最好自己使用回调函数。Nick Craver的回答很好地说明了这一点。事实上,它们不是未定义的(在代码执行之后)。通过跳过var
关键字,这些名称直接进入全局范围(在大多数情况下..是窗口
)。因此,在执行此脚本后,您可以从脚本的任何位置访问窗口
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
这很可能是你的问题,时间问题。由于这是ajax请求的成功处理程序
,因此此代码异步运行,因此不会立即执行
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );
要解决这个问题,最好自己使用回调函数。Nick Craver的回答对此进行了很好的演示。这将导致一个问题,因为$之后的数学将在回调之前执行。这将导致一个问题,因为$之后的数学将在回调之前执行。getJSON
之后的数学将在回调之前执行……我很好奇,谁会在没有投票的情况下否决这一点原因。不是我的否决票,但事实上它们是完全未定义的,除非早些时候有人创建并初始化了它们,它们在回调至少运行一次之前根本没有定义……我很好奇谁会无缘无故地否决它。不是我的否决票,但事实上,它们是完全未定义的,除非早些时候有人创建并初始化了它们,否则在回调至少运行一次之前,它们根本没有定义。
$.getJSON('/calculate_quote/' + moulding_1_id, function(data) {
moulding_1_cost = data.moulding.cost;
moulding_1_width = data.moulding.width;
});
cost_of_moulding = ( ( 2 * ( width + ( 2 * moulding_1_width ) ) + 2 * ( height + ( 2 * moulding_1_width ) ) ) / 1000 ) * moulding_1_cost;
$('#item_total').html( cost_of_moulding );