对于条件,仅在调用JS File时运行一次 [英] For condition running only once when calling JS File

查看:64
本文介绍了对于条件,仅在调用JS File时运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从c#代码运行Javascript.

I am running a Javascript from c# code.

  for(int i=0;i<=2;i++)
   {
        Page.ClientScript.RegisterStartupScript(GetType(), "a", "foo("hello")", true);
   }

Js文件

Function foo(a){
alert(a);
//some other logic
}

这里,问题是当我调试代码时,我可以看到循环进行了3次,但警报仅出现一次.

Here, the problem is when i debug the code, i can see loop going for 3 times, but the alert appears only once.

为什么只出现一次?我该如何解决?

Why does it appear only once ? How can I solve this ?

推荐答案

@Lloyd所说的是正确的,+ i是形成唯一对的必要条件.

What @Lloyd said is correct, the + i is necessary to make unique pairs.

尝试一下:

for (int i = 0; i <= 2; i++)
{
    Page.ClientScript.RegisterStartupScript(GetType(), "a"+ i, "foo('hello');", true);
}

您在javascript函数末尾缺少分号.

You were missing the semicolon at the end of the javascript function.

这就是@Lloyd建议产生的结果

This is what was being generated with what @Lloyd suggested

<script type="text/javascript">
//<![CDATA[
foo("hello")foo("hello")foo("hello")//]]>
</script>

这就是您想要的:

<script type="text/javascript">
//<![CDATA[
foo('hello');foo('hello');foo('hello');//]]>
</script>
</form>

这篇关于对于条件,仅在调用JS File时运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆