3

我试图在我看来注册一些 javascript,但我似乎遇到了问题。在我的母版页的底部,我有:

@(
    Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(@<text>
            // Open the hidden window when the feedback-link is clicked
            $('#feedback-link').click(function(e) {
                e.preventDefault();
                $('#FeedbackWindow').data('tWindow').center().open();
            });
        </text>)
)

在我看来,我想要一些特定于视图的 javascript,所以我有:

@(Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>)
)

不幸的是,这导致我的视​​图的 javascript 在 MVC 视图中以及在呈现最终页面时在我的母版页中都被声明。我怎样才能让它只注册一次脚本?

4

1 回答 1

4

正如我在我的论坛回复中所说的那样,ScriptRegistrar 被输出两次,因为被告知了。@()Razor 表达式将输出其内容,而将@{ }被执行。在您的情况下,您需要使用@{ }特定的脚本:

@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>);
}

另请注意,该@{ }块需要分号。

于 2011-05-03T08:53:48.197 回答