循环问题-JS
我有很多代码片段,例如:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要动态创建变量名,您必须使用括号表示法
,例如:
this['$m'+x]
或window['$m'+x]
将创建一个名为$m1
的变量,其中x = 1
尝试:
to create a variable name dynamicaly you have to use bracket notation
e.g:
this['$m'+x]
orwindow['$m'+x]
will create a variable called$m1
wherex = 1
try:
从
$m+x
的外观来看,我猜测您正在尝试动态创建一个变量,例如$m0
到$m9
基于从 0 到 10 的迭代。据我所知,你不能在 Javascript 中这样做,它会给你一个错误。我建议不要创建动态变量之类的东西,为什么不根据 x 的索引填充数组内的值。事情是这样的:
所以基本上
$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 ofx
.Here's something:
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.