我如何传递参数

发布于 2024-12-07 14:46:09 字数 2121 浏览 1 评论 0原文

仅举个例子

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

脱离于你 2024-12-14 14:46:09

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

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

machineId is scoped globally already, so it will be available for use directly in the ondblclick action. Also, you should add the var keyword before the machineId declaration.

var machineId = 150018;
paper = raphel('canvas_container', 15, 20);
rect = paper.rect().attr({.....});
rect.node.ondblclick = function() {
  window.open("graph.php?mach_id=" + machineId);     
}
几味少女 2024-12-14 14:46:09

如果将代码更改为

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

That is,而不是 id,只需传递 machineId 即可。

What if you change the code to

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

That is, instead of id, just pass along machineId.

暖伴 2024-12-14 14:46:09

只需将 id 更改为 machineId,您的代码就会按原样工作,因为您正在创建另一个可以访问作用域内变量的函数。

有关闭包和作用域的更多说明

Just change id to machineId 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

时光清浅 2024-12-14 14:46:09

这是什么问题:

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

如果(无法解释的)问题是您需要在定义 ondblclick 处理程序时保存 machineId 的值定义您需要一个闭包,而不是在处理程序执行时使用它的值:

(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({ }); 调用中的所有内容都位于该匿名函数内。当您的 success 和 ondblclick 处理程序在将来的某个时刻执行时,它们将使用自己的闭包中的 i 而不是循环索引。

What's wrong with this:

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

If the (unexplained) problem is that you need to save the value of machineId at the time the ondblclick handler is defined rather than using its value at the time the handler is executed, you need a closure:

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

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 time i equals whatever value it had when the loop ended. You need something like this:

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);
    }
}

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 the i from their own closure rather than the loop index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文