如何将参数传递给 jQuery document.ready() 函数(ASP.NET MVC,C#) [英] How to pass parameters to jQuery document.ready() function (ASP.NET MVC, C#)

查看:27
本文介绍了如何将参数传递给 jQuery document.ready() 函数(ASP.NET MVC,C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的视图中将一个参数传递给 jQuery document.ready() 函数:

I would like to pass a parameter to the jQuery document.ready() function from my View:

$(document).ready(function (parameter){
        $('select[name=Product]').val(parameter);
});

如何从我的视图触发事件并传递参数?我使用 Razor 作为查看引擎.

How can I fire the event from my View and pass the parameter? I use Razor as View engine.

谢谢

推荐答案

你不能.document.ready 函数不接受参数.例如,您可以将此参数定义为视图中的全局变量:

You can't. The document.ready function doesn't take parameters. You could for example define this parameter as a global variable in your view:

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
</script>

然后在您单独的 javascript 文件中使用此全局变量:

and then in your separate javascript file use this global variable:

$(function() {
    $('select[name=Product]').val(model.SomeProperty);
});

这篇关于如何将参数传递给 jQuery document.ready() 函数(ASP.NET MVC,C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆