简体   繁体   English

如何在循环中调用JavaScript函数

[英]How can I call JavaScript function in a loop

I would like to call JavaScript function many time . 我想多次调用JavaScript函数。 I have tried like that 我已经尝试过了
In Script 在脚本中

 function changeIt(strgr , idx) {
    //SomeCode
    return;  
 }    

In C# 在C#中

 protected void btn_Click(object sender, EventArgs e)  
 {  
      string strgr = 001;
      for(int i=0; i<3; i++)  
      {  
          base.RunScriptBottom("changeIt(" + strgr + "," + i + ");");  
      }
 }  

But it call the script function only one time. 但是它只调用脚本函数一次。 What could I do. 我能做什么。
With regards 带着敬意

Check ClientScriptManager.RegisterStartupScript 检查ClientScriptManager.RegisterStartupScript

By the way, you can write a js function to loop and call from server side once.. 顺便说一句,您可以编写一个js函数来循环并从服务器端调用一次。

 function changeAll(strgr , from, to) {

   for(int i = from, i< to; i++)
      changeIt(strgr ,i);
 } 

Server Side: 服务器端:

protected void btn_Click(object sender, EventArgs e)  
 {  
      string strgr = 001;

          base.RunScriptBottom("changeAll(" + strgr + ",0,3);");  

 } 

You should add a client event to do this 您应该添加一个客户端事件来执行此操作

<asp:Button Text="text" runat="server" OnClientClick="loop()" />
<script type="text/javascript">
    function loop() {
        var strgr = 001;
        for(var i=0; i<3; i++)  
        {  
            changeIt(strgr + "," + i );  
        }
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM