在javascript中无法访问j里面的for循环。怎么做? [英] Can't access j inside for loop in javascript. How to do?

查看:85
本文介绍了在javascript中无法访问j里面的for循环。怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在此行中访问for循环中的j



Can't access j inside for loop in this line

var points = @Html.Raw(Json.Encode(@Model.objDllCollection[j].Parameters)) 





错误我得到的是





the error which i'm getting is

The name j does not exist in the current context







function onAdditional() {
        var j;
        for (j = 0; j < @Model.objDllCollection.Count(); j++) {
            debugger
            var ParamArray = new Array();
            var points = @Html.Raw(Json.Encode(@Model.objDllCollection[j].Parameters))
            ParamArray.push(points);

          
        }

    }

推荐答案

之后的代码 @在生成的html发送到浏览器之前执行服务器端。当html在浏览器中运行时,j作为javascript变量存在,因此您不能在服务器端代码中使用它,因为它在该上下文中不存在。你需要使用服务器代码构造所有js,类似这样的事情



The code after "@" is executed server-side before the resulting html is sent to the browser. "j" exists as a javascript variable when the html runs in the browser, so you can't use it in your server-side code as it doesn't exist in that context. You need to construct all of the js using server code, something like this

function onAdditional() {
    debugger;
    var ParamArray = new Array();
    var points;

    @foreach(var m in Model.objDllCollection)
    {
        string p = string.Format("points = {0};\r\nParamArray.push(points);\r\n", Json.Encode(m.Parameters));
        @Html.Raw(p);
    } 
}


这篇关于在javascript中无法访问j里面的for循环。怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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