循环问题-JS

发布于 2024-11-07 21:43:05 字数 1898 浏览 0 评论 0原文

我有很多代码片段,例如:

<script type="text/javascript">
dojo.query("body").delegate("#input0 > select.estatistica", "onchange", function(evt){
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m0 = [];

                for (var i = 1; i < 10; i++) {
                    $m0.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChar0);

            }
        });
    });
</script>


<script type="text/javascript">
dojo.query("body").delegate("#input1 > select.estatistica", "onchange", function(evt){
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m1 = [];

                for (var i = 1; i < 10; i++) {
                    $m1.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChart1);
            }
        });
    });
</script>

我尝试了这个循环,但我不确定脚本。可能我有语法错误。

<script type="text/javascript">
for(x=0; x<10; x++) {
dojo.query("body").delegate("'#input'+x+'> select.estatistica'", "onchange", function(evt) {
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m+x = [];

                for (var i = 1; i < 10; i++) {
                    $m+x.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChart+x);
            }
        });
    });
}
</script>

谢谢

i have many pieces of code like:

<script type="text/javascript">
dojo.query("body").delegate("#input0 > select.estatistica", "onchange", function(evt){
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m0 = [];

                for (var i = 1; i < 10; i++) {
                    $m0.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChar0);

            }
        });
    });
</script>


<script type="text/javascript">
dojo.query("body").delegate("#input1 > select.estatistica", "onchange", function(evt){
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m1 = [];

                for (var i = 1; i < 10; i++) {
                    $m1.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChart1);
            }
        });
    });
</script>

i tried this loop, but i am not sure about the script. Probably i have syntax errors.

<script type="text/javascript">
for(x=0; x<10; x++) {
dojo.query("body").delegate("'#input'+x+'> select.estatistica'", "onchange", function(evt) {
        dojo.xhrPost({
            url: "drop2.php",
            handleAs: "json",
            postData: "data=" + $(this).val(),
            preventCache: true,
            load: function(json) {
                $m+x = [];

                for (var i = 1; i < 10; i++) {
                    $m+x.push(parseFloat(json[i]["valor" + i]));
                }
                dojo.addOnLoad(refreshChart+x);
            }
        });
    });
}
</script>

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不必了 2024-11-14 21:43:05

要动态创建变量名,您必须使用括号表示法

,例如: this['$m'+x]window['$m'+x] 将创建一个名为 $m1 的变量,其中 x = 1

尝试:

window['foo' + 'bar'] = 'hello';
alert(foobar);

to create a variable name dynamicaly you have to use bracket notation

e.g: this['$m'+x] or window['$m'+x] will create a variable called $m1 where x = 1

try:

window['foo' + 'bar'] = 'hello';
alert(foobar);
羁〃客ぐ 2024-11-14 21:43:05

$m+x 的外观来看,我猜测您正在尝试动态创建一个变量,例如 $m0$m9基于从 0 到 10 的迭代。据我所知,你不能在 Javascript 中这样做,它会给你一个错误。我建议不要创建动态变量之类的东西,为什么不根据 x 的索引填充数组内的值。

事情是这样的:

var $m = [];
for(x=0; x<10; x++)
{
    // some of your codes here ... 

    // make $m[x] an array
    if (typeof $m[x] == "undefined")
        $m[x] = [];

    // some of your codes here...

    for (var i = 1; i < 10; i++)
    {
        // of course you need to change this to what you need to push
        $m[x].push(i);
    }

}

console.log($m[0]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

所以基本上 $m 将是一个带有数组的数组。我不知道我的猜测是否正确,但我希望它能提供一个想法。

By the looks of $m+x I'm guessing that you're trying to create a variable dynamically such as $m0 to $m9 based on iterating from 0 - 10. You can't do that in Javascript as far as I'm aware it will give you an error. I suggest instead of creating a dynamic variable sort of thing why not fill the values inside an array based on the indexes of x.

Here's something:

var $m = [];
for(x=0; x<10; x++)
{
    // some of your codes here ... 

    // make $m[x] an array
    if (typeof $m[x] == "undefined")
        $m[x] = [];

    // some of your codes here...

    for (var i = 1; i < 10; i++)
    {
        // of course you need to change this to what you need to push
        $m[x].push(i);
    }

}

console.log($m[0]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

So basically $m will be an array with arrays. I don't know if my guess is right but I hope it gives an idea.

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