我怎样才能传递参数

时间:2011-09-29 12:51:09

标签: javascript raphael

仅举例

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;

我需要的结果是打开新窗口,其中包含网址http://localhost/graph.php?mach_id=150018;


来自重复问题的

代码

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

4 个答案:

答案 0 :(得分:0)

这有什么问题:

window.open("graph.php?mach_id=" + machineId);

如果(原因不明)问题是您需要在machineId处理程序定义时保存ondblclick的值,而不是在当时使用其值处理程序是执行,你需要一个闭包:

(function (id) {
   rect.node.ondblclick = function() {
     window.open("graph.php?mach_id=" + id);     
   }
})(machineId);

更新:好的,根据您发布的新代码,我可以看到我在上面的闭包构思上走在正确的轨道上。问题是您的事件处理程序尝试在将来的某个时刻使用i索引,此时i等于循环结束时的任何值。你需要这样的东西:

function showUtilization(machineId, rectUtil, txtResult, txtMCName, rectCover) {
    for (var index = 0; index < machineId.length; index++) {
        (function(i){
           $.ajax({
               // all of your other code here, where anytime it
               // uses i it will be using the parameter of the
               // anonymous function rather than the loop counter
               // (which I've renamed to "index" to avoid confusion
               // with "i")
           });
        })(index);
    }
}

我已经将循环更改为使用名为index的计数器,该计数器将传递给立即执行的匿名函数。您现有的代码,即$.ajax({ });调用中的所有内容都在匿名函数内部。当您的成功和ondblclick处理程序在将来的某个时刻执行时,它们将使用自己的闭包中的i而不是循环索引。

答案 1 :(得分:0)

machineId已全局范围,因此可以直接在ondblclick操作中使用。另外,您应该在var声明之前添加machineId关键字。

var machineId = 150018;
paper = raphel('canvas_container', 15, 20);
rect = paper.rect().attr({.....});
rect.node.ondblclick = function() {
  window.open("graph.php?mach_id=" + machineId);     
}

答案 2 :(得分:0)

如果您将代码更改为

,该怎么办?
machineId = 150018;
paper = raphel('canvas_container', 15, 20);
rect = paper.rect().attr({.....});
rect.node.ondblclick = function() {
  window.open("graph.php?mach_id=" + machineId);     
}

即代替id,只需传递machineId

答案 3 :(得分:0)

只需将id更改为machineId,您的代码就可以正常工作,因为您正在创建另一个可以访问范围内变量的函数。

More explanation on closures and scopes

相关问题