如何在Json中创建泛型函数

时间:2012-11-20 13:34:16

标签: asp.net-mvc json

我在视图中有这段代码

<a href="#SampleEditor" onclick="javaScript:ShowSampleEditor()"
                    id="SampleLink">Add Sample</a>
                <div class="editor-field" id="SampleEditor" style="display: none">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.SampleCollectionInstructions)
                    </div>
                    <div class="editor-field">
                        @Html.TextAreaFor(model => model.SampleCollectionInstructions, new { @class = "adminRichText" })
                        @Html.ValidationMessageFor(model => model.SampleCollectionInstructions)
                    </div>
                </div>

它的作用是向用户显示一个链接以打开富文本编辑器并隐藏链接

这是我使用的代码

function ShowSampleEditor() {
        $("#SampleEditor").show();
        $("#SampleLink").hide();
    }

现在我必须为更多的编辑做这件事。由于Json不是我的事,我如何为sevearal编辑器制作一个通用函数呢?

1 个答案:

答案 0 :(得分:0)

将代码编辑为以下内容

 <a href="#SampleEditor" onclick="javaScript:ShowSampleEditor(this)"
                id="SampleLink">Add Sample</a>
            <div class="editor-field" id="SampleEditor" style="display: none">
                <div class="editor-label">
                    @Html.LabelFor(model => model.SampleCollectionInstructions)
                </div>
                <div class="editor-field">
                    @Html.TextAreaFor(model => model.SampleCollectionInstructions, new { @class = "adminRichText" })
                    @Html.ValidationMessageFor(model => model.SampleCollectionInstructions)
                </div>
            </div>

和javascript到

function ShowSampleEditor(link) {
   $(link).next().show(); // Next element after the link is the panel
   //$(".editor-field").show(); // Use this if the element to show does not immediately follow the link
   $(link).hide();
}

http://jsfiddle.net/27MeG/8/

看一个演示小提琴

我已经将javascript函数概括为将点击的链接作为参数。这个链接是隐藏的,它是下一个兄弟节目 - 应该是编辑。