呼叫剃刀的子视图里面jQuery函数 [英] Call Jquery function inside razor's subviews
问题描述
我想用这样的活动 $('#myModal)显示();
的原始调用插件程序如下:
The original calling plugin routine is as follows:
<script type="text/javascript">
$(document).ready(function() {
$('#myButton').click(function(e) {
e.preventDefault();
$('#myModal').show();
});
});
</script>
我把我的code在下面的部分。
在这里, $('#myModal')显示({});
不运行
@using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { name = "send-contact", id = "contactform1" }))
{
@Html.AntiForgeryToken()
if (!String.IsNullOrEmpty(stResult))
{
<text>
<div id="myModal" class="reveal-modal">
<h1>Modal Title</h1>
<p>Any content could go in here.</p>
<a class="close-reveal-modal">×</a>
</div>
$('#myModal').show({}); // The desired function!
</text>
}
}
有我的code在2个问题,这种方法的
There are 2 problems on this approach within my code
-
在 jQuery库位于母版页的底线(_layout)
有一个包装在其上设在那里著名的就绪()
函数内所有的插件。
The JQuery library is located at the bottom line of the Master Page (_Layout) There's a wrapper over all the plugins inside the famous
ready()
function which is located there.
作为一种替代办法,我放在 $('#myModal)显示();
的 JQuery.ready内( )
在是否应该已经找到了#myModal
根据我的情况,然后它会触发功能时,它调用一切该包装我想要的。
但同样不会触发。
As an alternative way I placed the $('#myModal').show();
inside the JQuery.ready()
in the Wrapper which when it calls everything if it should have found the "#myModal"
based on my conditions then it'll fire the function I desired.
but again that won't fire.
我觉得这个方法似乎更合理,更可能是唯一合乎逻辑的这些方法2导致第一个功能是不位于 JQuery的准备函数,如果我用另一种这里准备功能,那是因为准备功能重复的也是错误的。
I think this approach seems to be more logical and may be the only logical between these 2 approaches cause the first one the function isn't located in the JQuery ready function, and if I use another ready function here it's also wrong because of the duplicate of ready function.
推荐答案
如果您在包括 Foo.cshtml
称为局部视图,你也可以创建一个名为局部视图 FooScripts.cshtml
和渲染了您的脚本的其余部分加载得到该视图:
If you are including a Partial view called Foo.cshtml
you could also create a partial view named FooScripts.cshtml
and render that view where the rest of your scripts are getting loaded:
<h2>Main View</h2>
@Html.Partial("Foo")
...
@section scripts {
@Scripts.Render("~/bundles/jquery")
@Html.Partial("FooScripts")
}
这篇关于呼叫剃刀的子视图里面jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!