在 MVC 5 Razor 视图中调用 JavaScript 函数 [英] Calling JavaScript function in MVC 5 Razor view

查看:34
本文介绍了在 MVC 5 Razor 视图中调用 JavaScript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一篇文章中看到,您可以像这样在 razor 代码中调用 JavaScript 函数:

@:FunctionName()

对我来说,虽然这只会输出实际的单词 FunctionName()

这是我的观点:

@model PriceCompare.Models.QuoteModel@{ViewBag.Title = "报价";}<h2>引用</h2>@if (@Model.clarify == true){//做下拉 loic@:ShowClarify();}别的{//填写引用@:ShowQuote();}<div class="澄清">你可以看到澄清的div

<div class="quote">你可以看到报价div

@节头{<script type="text/javascript">$(文件).准备好(函数 ShowQuote() {$(".quote").show();},函数 ShowClarify() {$(".clarify").show();});}

是不是因为我把它嵌套在了‘@if’中?无论如何?

解决方案

你需要把你的 javascript 放在一个

I have seen in another post that you can call a JavaScript function in your razor code like so:

@:FunctionName()

For me though this only outputs the actual words FunctionName()

Here is my view:

@model PriceCompare.Models.QuoteModel

@{
    ViewBag.Title = "Quote";
}

<h2>Quote</h2>

@if (@Model.clarify == true)
{
    // do drop down loic
    @:ShowClarify();
}
else
{
    // fill quote
    @:ShowQuote();
}
<div class="clarify">

    You can see the clarify div
</div>
<div class="quote">

    You can see the quote div
</div>

@section head {

    <script type="text/javascript">

        $(document).ready(
            function ShowQuote() {
                $(".quote").show();
            },
            function ShowClarify() {
                $(".clarify").show();
            }
        );

    </script>
}

Is it because I have nested it in an `@if'? Anyway around this?

解决方案

You need to put your javascript in a <script> tag, and you need to call the functions within their scope:

<script type="text/javascript">

    $(document).ready(
        function ShowQuote() {
            $(".quote").show();
        },
        function ShowClarify() {
            $(".clarify").show();
        }

        @if (@Model.clarify == true)
        {
            // do drop down loic
            ShowClarify();
        }
        else
        {
            // fill quote
            ShowQuote();
        }
    );

</script>

这篇关于在 MVC 5 Razor 视图中调用 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆