为什么这个 JQuery 无效?

发布于 2024-09-07 04:28:26 字数 620 浏览 5 评论 0原文

我一直试图找出为什么这是无效的(根据 VS2008)。

//Global variable
var sortFields;

$(document).ready(function() {
    sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
    //Other Code here...
});

我的 HtmlHelper 代码

    public static string ToJson(object obj)
    {
        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(obj);

        return json;
    }

Helper 正在生成有效的 Json(已确认),但是当我尝试向函数添加其他任何内容时,VS2008 会抱怨各种问题,在关闭括号时无法正确对齐代码等等,一旦我注释掉它,它就起作用了。然而,即使 VS2008 抱怨,代码也可以正常工作。 这只是VS2008对JQuery来说是垃圾还是我实际上做错了什么?

I've been trying to figure out why this isn't valid (according to VS2008).

//Global variable
var sortFields;

$(document).ready(function() {
    sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
    //Other Code here...
});

My HtmlHelper code

    public static string ToJson(object obj)
    {
        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(obj);

        return json;
    }

The Helper is generating valid Json (confirmed it), but when I'm trying to add anything else to the function, VS2008 complains about all sort kinds of stuff, can't align the code correctly and so on when closing brackets, and as soon as I comment this out it works. However, the code works fine, even if VS2008 complains about it.
Is this just VS2008 that is crap with JQuery or am I actually doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉态萌生 2024-09-14 04:28:26

<%=出现在脚本中时,设计者通常会感到有点不安(当它出现在属性值中时,有时会变得混乱)。如果输出正确,并且您对 html 的构造方式感到满意,那么我就不会强调。

但最终; VS 如何知道 ToJson 将返回一些合理的内容?它可能会返回 "(((((("),这会真正搞砸 JavaScript。这就是它不高兴的原因。

The designer is generally going to get a bit upset when <%= is in script (and it sometimes gets snarly when it is in attribute values). If the output is correct, and you're happy with the way the html is constructed, then I wouldn't stress.

Ultimately, though; how does VS know that ToJson is going to return something sensible? It could return "(((((((" which would really screw up the javascript. That is why it is unhappy.

自我难过 2024-09-14 04:28:26

MrW,

你这里缺少一个括号:

$(document).ready(function() {
    sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
    //Other Code here...
};

它应该是:

$(document).ready(function() {
    sortFields = '<%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>';
    //Other Code here...
**)};**

另外,返回一个 jsonresult 可能会更好?另外,在 CustomHtmlHelper 周围添加单引号,如上所示。

MrW,

you,ve got a missing bracket here:

$(document).ready(function() {
    sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
    //Other Code here...
};

it should be:

$(document).ready(function() {
    sortFields = '<%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>';
    //Other Code here...
**)};**

also, it may be better to return a jsonresult?? Also, add single quotes around the CustomHtmlHelper as shown above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文