我如何传递参数
仅举个例子
machineId = 150018;
paper = raphel('canvas_container', 15, 20);
rect = paper.rect().attr({.....});
rect.node.ondblclick = function() {
window.open("graph.php?mach_id=" + id);
}
问题:如何将 machineId 值传递给 id;
我需要的结果是使用 url http://localhost/graph.php?mach_id=150018< 打开新窗口/a>;
来自重复问题的代码
function showUtilization(machineId, rectUtil, txtResult, txtMCName, rectCover) {
for (i = 0; i < machineId.length; i++) {
$.ajax({
url: 'ajax/getonemachineinfo.php',
data: { id: machineId[i] },
dataType: 'text',
async: false,
success: function(data) {
results = data.split(',');
status = results[0];
utilize = results[1];
// Machine Name
switch (status) {
case '0': var colorCode = "#FF0000"; break;
case '1': var colorCode = "#33CC33"; break;
case '2': var colorCode = "#808080"; break;
}
txtMCName[i].attr({ fill: colorCode });
// utilization
rectUtil[i].attr({ width: (utilize * conversionFactor())/100 });
if (utilize <= 30) {
var colorAttr = [{ fill: "#FF0000" }];
} else if ((utilize > 30) && (utilize <= 60)) {
var colorAttr = [{ fill: "#FFFF00" }];
} else if (utilize > 60) {
var colorAttr = [{ fill: "#33CC33" }];
}
rectUtil[i].attr(colorAttr);
txtResultAttr = [{ text: utilize + '%'}];
txtResult[i].attr(txtResultAttr);
txtResult[i].attr(colorAttr);
rectCover[i].node.ondblclick = function() {
window.open("graph.php?mach_id=" + machineId[i]);
}
}
});
}
} (2
Just for examples
machineId = 150018;
paper = raphel('canvas_container', 15, 20);
rect = paper.rect().attr({.....});
rect.node.ondblclick = function() {
window.open("graph.php?mach_id=" + id);
}
Question: How can I pass machineId value to id;
I need the result is to open new window with url http://localhost/graph.php?mach_id=150018;
code from duplicate question
function showUtilization(machineId, rectUtil, txtResult, txtMCName, rectCover) {
for (i = 0; i < machineId.length; i++) {
$.ajax({
url: 'ajax/getonemachineinfo.php',
data: { id: machineId[i] },
dataType: 'text',
async: false,
success: function(data) {
results = data.split(',');
status = results[0];
utilize = results[1];
// Machine Name
switch (status) {
case '0': var colorCode = "#FF0000"; break;
case '1': var colorCode = "#33CC33"; break;
case '2': var colorCode = "#808080"; break;
}
txtMCName[i].attr({ fill: colorCode });
// utilization
rectUtil[i].attr({ width: (utilize * conversionFactor())/100 });
if (utilize <= 30) {
var colorAttr = [{ fill: "#FF0000" }];
} else if ((utilize > 30) && (utilize <= 60)) {
var colorAttr = [{ fill: "#FFFF00" }];
} else if (utilize > 60) {
var colorAttr = [{ fill: "#33CC33" }];
}
rectUtil[i].attr(colorAttr);
txtResultAttr = [{ text: utilize + '%'}];
txtResult[i].attr(txtResultAttr);
txtResult[i].attr(colorAttr);
rectCover[i].node.ondblclick = function() {
window.open("graph.php?mach_id=" + machineId[i]);
}
}
});
}
} (2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
machineId
已经具有全局作用域,因此可以直接在ondblclick
操作中使用。另外,您应该在machineId
声明之前添加var
关键字。machineId
is scoped globally already, so it will be available for use directly in theondblclick
action. Also, you should add thevar
keyword before themachineId
declaration.如果将代码更改为
That is,而不是
id
,只需传递machineId
即可。What if you change the code to
That is, instead of
id
, just pass alongmachineId
.只需将
id
更改为machineId
,您的代码就会按原样工作,因为您正在创建另一个可以访问作用域内变量的函数。有关闭包和作用域的更多说明
Just change
id
tomachineId
and your code would work as it is, since you are creating an another function which has access to variables within the scope.More explanation on closures and scopes
这是什么问题:
如果(无法解释的)问题是您需要在定义
ondblclick
处理程序时保存machineId
的值定义您需要一个闭包,而不是在处理程序执行时使用它的值:更新:好的,根据您发布的新代码,我可以看到我在正确的轨道与上面的封闭思想。问题是您的事件处理程序尝试在将来的某个时刻使用
i
索引,此时i
等于循环结束时它所具有的任何值。您需要这样的东西:我已经更改了循环以使用名为
index
的计数器,该计数器被传递到立即执行的匿名函数。您现有的代码,即$.ajax({ });
调用中的所有内容都位于该匿名函数内。当您的 success 和 ondblclick 处理程序在将来的某个时刻执行时,它们将使用自己的闭包中的i
而不是循环索引。What's wrong with this:
If the (unexplained) problem is that you need to save the value of
machineId
at the time theondblclick
handler is defined rather than using its value at the time the handler is executed, you need a closure:UPDATE: OK, based on the new code you posted I can see I was on the right track with the closure idea above. The problem is that your event handlers try to use the
i
index at some point in the future at which timei
equals whatever value it had when the loop ended. You need something like this:I've changed the loop to use a counter called
index
, which is passed in to an anonymous function that is immediately executed. Your existing code, i.e., everything that you have inside the$.ajax({ });
call goes inside that anonymous function. When your success and ondblclick handlers are executed at some point in the future they will use thei
from their own closure rather than the loop index.