$ .data未定义

时间:2013-01-06 11:24:27

标签: asp.net-mvc json

以下是我的MVC剃须刀视图的标记:

<div style="visibility:visible;">
var data = [
        {"id":"1", "ui_bgcolor":"Green", "ui_forecolor": "Black"},
        {"id":"2", "ui_bgcolor":"Amber", "ui_forecolor": "Black"},
        {"id":"3", "ui_bgcolor":"Red", "ui_forecolor": "White"}
]
</div>

<script type="text/javascript">
    function SetGradeDLLItemStyling() {
        $.each(data, function (i) {
            alert(data[i].UIBackgroundColor);
        });
    }
</script>

我收到$ .data是脚本块中未定义的错误。这有什么不对?

1 个答案:

答案 0 :(得分:2)

数据未在脚本块中定义,因此无法使用。

<script type="text/javascript">
var data = [
        {"id":"1", "ui_bgcolor":"Green", "ui_forecolor": "Black"},
        {"id":"2", "ui_bgcolor":"Amber", "ui_forecolor": "Black"},
        {"id":"3", "ui_bgcolor":"Red", "ui_forecolor": "White"}
]
</script>