【问题标题】:When running javascript loop with internal call to code behind, the code behind runs only the first time当运行带有内部代码调用的javascript循环时,后面的代码只运行第一次
【发布时间】:2016-05-25 08:27:07
【问题描述】:

当运行 Javascript 循环时,用数据调用后面的代码,只显示第一行。我需要每次获取另一个值而不重新加载页面。

示例如下:

for (var counter = 0; counter < 50; counter++) {
  val = <%=  GetLon("500") %>;
  alert (val);
}

它只调用一次!!

【问题讨论】:

  • 如果您不发布代码,您希望我们如何提供帮助?
  • 不!该代码工作 50 次,但它只调用 GetLon 1 次
  • GetLon 是异步的吗?
  • c#后面代码中的一个简单函数

标签: javascript code-behind


【解决方案1】:

好的。我找到了这个response

您可以使用 XMLHTTPRequest 调用服务器端代码页。一次 服务端处理完代码,会回调给客户端 处理后的页面。然后你使用回调函数来拉 您需要从结果中获取的任何数据。整个过程称为 AJAX。

function ajaxExample() {
    var xmlhttp;
  xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("someID").innerHTML=xmlhttp.responseText;
    }
  }

    xmlhttp.open("GET","ajax.asp?q="+str,true);
    xmlhttp.send();
}

我还没有测试它,但看起来是正确的答案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2011-06-27
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多